0
考慮一個MATLAB結構用Matlab「聯盟」功能,我有一個環比的字段名如下:在一個循環
scenario = fieldnames(myStruct)
for scenarioidx = 1:length(scenario)
scenarioname = scenario{scenarioidx};
category = fieldnames(myStruct.(scenarioname))
for categoryidx = 1:length(category)
categoryname = category{categoryidx};
entity = fieldnames(myStruct.(scenarioname).(categoryname))
end
end
這個循環返回我對每個類別的實體。現在,我想將所有這些實體合併到一個向量中。我試圖用「聯盟」的功能如下:
scenario = fieldnames(myStruct)
for scenarioidx = 1:length(scenario)
scenarioname = scenario{scenarioidx};
category = fieldnames(myStruct.(scenarioname))
for categoryidx = 1:length(category)
categoryname = category{categoryidx};
allEntity = {}
entity = fieldnames(myStruct.(scenarioname).(categoryname))
combo_entity = union (allEntity, entity)
end
end
不幸的是,這只是返回相同的結果之前,而不是結合東西。 有沒有人有關於如何在這樣的循環中實現聯合功能的任何想法?
謝謝你這樣做的工作!不幸的是我需要在這個循環中實現聯合功能! – steve
@steve我不明白。當你說你想*實現*'union'時,你的意思是你想要定義這個函數,或者只是使用標準函數? 'union'是一個標準函數,並且與set union有關。它僅適用於數字數組或字符串的單元數組。所有的「實體」事物都是數字,字符串還是其他東西? –
我只需要使用標準的** union **功能。在運行我的**域名**循環時,**實體**是n×1單元格。 – steve