我是MATLAB的初學者,希望能在這個漂亮的網站上找到幫助(再:) :)。 這是關於一系列個體的模型預測結果。因此,我有一系列 變量(含有每個單獨的基本結果),並且它們的命名法僅在第一部分..e不同 ,G:構建一個循環函數中的兩個字符串值的變量名稱
麥克結果保存爲:Mike_Sim_V_software1 亞當結果保存爲:Adam_Sim_V_sofwtare1 薩拉結果保存爲:Sarah_Sim_V_sofwtare1 等等...
我已經有名稱的列表[「邁克」,「亞當」,「莎拉」,等等] ID(%保存在變量名爲:idx)
因爲我想執行一系列計算對於所有上述變量(結果變量), 相似,爲我的變量的名字在他們的第一部分區別僅在於,我覺得寫這樣的循環功能:
for idx=1:80
x= Indi_All {idx,1} (1,1)
% idx: is the Individual IDs ... 80 is the total number of individuals
% Indi_All is a cell array containing the basic info, IDs and demographics..
here x will get the initial part of the variables names e.g. 'Mike' or 'Adam'
Indi_Results {idx,1} = x
Indi_Results {idx,2} = prctile (x_Sim_V_software1', (5))
Indi_Results {idx,2} = x_5P_software1'
Indi_Results {idx,3} = prctile (x_Sim_V_software1', (10))
Indi_Results {idx,3} = x_10P_software1'
Indi_Results {idx,4} = prctile (x_Sim_V_software1', (25))
Indi_Results {idx,4} = x_25P_software1'
Indi_Results {idx,5} = prctile (x_Sim_V_software1', [50])
Indi_Results {idx,5} = x_Median_software1'
Indi_Results {idx,6} = prctile (x_Sim_V_software1', (75))
Indi_Results {idx,6} = x_75P_software1'
Indi_Results {idx,7} = prctile (x_Sim_V_software1', (90))
Indi_Results {idx,7} = x_90P_software1'
Indi_Results {idx,8} = prctile (x_Sim_V_software1', [95])
Indi_Results {idx,8} = x_95P_software1'
% the code and calculations go even more and more ...
end
以便專家能請參閱... x將在代碼(右列)中被識別爲'x',而不是我實際希望它的意思,即變量x包含字符串值,如:'Mike'。
我的問題是:
1)我可以解決這個問題嗎?是否有任何函數來構造來自兩個不同字符串的變量的名稱?所以可能類似於num2str,但要將字符串添加到字符串! 換句話說,要將不斷變化的部分'Mike','Adam'等添加到不變的部分'_Sim_V_software1'中,以獲得一個我可以在循環函數中使用的變量?!
我不知道......我有這樣的感覺,我的問題很愚蠢,但不知何故,我花了很多時間來完成這個任務,而且這不是我想要的樣子! 希望這與我的大腦已經知道一些關於週末開始的事實無關:)
我會很高興聽到你的東西,並提前感謝大家......!
更新1: @KlausCPH 我必須修改它以獲得我想要的第一行..像這樣:Indi_Results {idx,1} = eval('x') 但我找不出如何修改第二行它是,我得到以下錯誤消息:未定義的函數'eval'輸入參數的類型'細胞'。 和 如果我修改這種方式:EVAL([「X」「_5P_software1」])..我得到這個錯誤消息: 未定義函數或變量「x_5P_software1」 ..(x被識別爲單個信! ) 可以請你再幫忙嗎?! –
好的。這可能是因爲x中的字符串在單元格中。嘗試改變你的行'Indi_All {idx,1}(1,1)'到'Indi_All {idx,1} {1,1}'。如果這不起作用,那麼找出x是什麼類型並將其發佈在這裏。 – KlausCPH
非常感謝回覆..我再次檢查它,x在一個單元格...所以我改變了代碼:Indi_Results {idx,2} = prctile(eval([x {1,1}'_Sim_V_software1' ])',(5))...它工作完美...再次感謝 –