2013-01-18 43 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

相关问题