2013-01-18 47 views
1

我是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'中,以獲得一個我可以在循環函數中使用的變量?!

我不知道......我有這樣的感覺,我的問題很愚蠢,但不知何故,我花了很多時間來完成這個任務,而且這不是我想要的樣子! 希望這與我的大腦已經知道一些關於週末開始的事實無關:)

我會很高興聽到你的東西,並提前感謝大家......!

回答

2

其實很簡單。您需要使用eval()功能是這樣的:

Indi_Results {idx,1} = eval(x) 
Indi_Results {idx,2} = prctile (eval([x '_Sim_V_software1'])', (5)) 
Indi_Results {idx,2} = eval([x '_5P_software1'])' 
... 

字符串連接,你可以使用[STR1 STR2]如上也顯示。

問候

+0

更新1: @KlausCPH 我必須修改它以獲得我想要的第一行..像這樣:Indi_Results {idx,1} = eval('x') 但我找不出如何修改第二行它是,我得到以下錯誤消息:未定義的函數'eval'輸入參數的類型'細胞'。 和 如果我修改這種方式:EVAL([「X」「_5P_software1」])..我得到這個錯誤消息: 未定義函數或變量「x_5P_software1」 ..(x被識別爲單個信! ) 可以請你再幫忙嗎?! –

+0

好的。這可能是因爲x中的字符串在單元格中。嘗試改變你的行'Indi_All {idx,1}(1,1)'到'Indi_All {idx,1} {1,1}'。如果這不起作用,那麼找出x是什麼類型並將其發佈在這裏。 – KlausCPH

+0

非常感謝回覆..我再次檢查它,x在一個單元格...所以我改變了代碼:Indi_Results {idx,2} = prctile(eval([x {1,1}'_Sim_V_software1' ])',(5))...它工作完美...再次感謝 –

1

使用eval是可能的,但可能沒有必要。

相反,我建議你將文件讀入單元陣列。然後通過索引到數組中來操作它們。例如,

fn = {'Mike','Adam','Sarah'}; 


for i=1:length(fn) 
    % use the functional form of the load() function 
    % to get the time series into t{i} regardless of the filename. 

    t{i} = load([fn '_Sim_V_software1 ']); 

    res(i, :) = prctile (t{i}, [5 10 25 50 75 90 95]); 
end 

在上面的例子中,你並不真正需要t{}單元陣列,但我假設你希望做更多的分析時間序列。

+0

我不確定變量來自可加載的文件。據我所知,至少它不在任何地方。 – KlausCPH

+0

@KlausCPH:OP特別說「Mike結果另存爲...」 – nimrodm

+0

確實如此。但是,之前他們被稱爲「變量」。但是在討論OP時沒有必要討論這個問題,就像我們可以解決這個問題一樣:-)。所以Leo ...:你的數據集是源自具有所提及名稱的文件,還是它們是變量(可能由其他腳本生成)? – KlausCPH

相關問題