String 从循环函数中的两个字符串值构造变量名
我是MATLAB的初学者,我希望我能在这个不错的网站上找到帮助。 它是关于一系列个体的模型预测结果。因此,我有一系列的建议 变量(包含每个个体的基本结果)及其命名方式不同 仅在第一部分中..例如: Mike结果另存为:Mike_Sim_V_软件1 Adam结果另存为:Adam_Sim_V_sofwtare1 Sarah结果另存为:Sarah_Sim_V_sofwtare1 等等 我已经有了名字列表['Mike'、'Adam'、'Sarah'等]和ID(%保存在名为:idx的变量中) 因为我想对上述所有变量(结果变量)进行一系列类似的计算, 由于变量的名称仅在第一部分有所不同,我考虑编写如下循环函数:String 从循环函数中的两个字符串值构造变量名,string,matlab,variables,loops,String,Matlab,Variables,Loops,我是MATLAB的初学者,我希望我能在这个不错的网站上找到帮助。 它是关于一系列个体的模型预测结果。因此,我有一系列的建议 变量(包含每个个体的基本结果)及其命名方式不同 仅在第一部分中..例如: Mike结果另存为:Mike_Sim_V_软件1 Adam结果另存为:Adam_Sim_V_sofwtare1 Sarah结果另存为:Sarah_Sim_V_sofwtare1 等等 我已经有了名字列表['Mike'、'Adam'、'Sarah'等]和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
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'])'
...
所以专家们可以看到。。。在代码(右列)中,x将被识别为“x”,而不是我实际希望它的意思,即变量x,它包含一个字符串值,如:“Mike”
我的问题是:
1) 我能解决这个问题吗?有没有函数可以从两个不同的字符串构造变量名?!所以,也许是类似于num2str的东西,但要在字符串中添加字符串!
换言之,将变化部分“Mike”、“Adam”等添加到不变部分“Sim\u V\u software1”中,以获得一个变量,我可以在循环函数中使用它
我不知道。。。我觉得我的问题很傻,但不知怎么的,我花了很多时间在这个问题上,它没有按照我想要的方式工作!
希望这与我的头脑已经知道周末即将开始这一事实无关:)
我很高兴听到你的消息,并提前表示感谢 其实很简单。您需要像这样使用
eval()
函数:
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
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],如上所示
关于使用
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{}
实际上不需要单元格数组,但我假设您希望对时间序列进行更多分析。我不确定变量是否来自可以加载的文件。至少就我所知,它并没有出现在任何地方。@KlausCPH:OP明确地说“Mike结果保存为…”是真的。然而,早些时候它们被称为“变量”。但是,当OP讨论这个问题时,没有任何意义,正如我们可以澄清这一点一样:-)。所以Leo…:你们的数据集是来自具有上述名称的文件,还是它们是变量(可能由其他脚本生成)?伙计们,非常感谢你们的回答,非常抱歉,因为响应太晚了。。。前面提到的数据已经保存为变量。。。。(我已经使用xlsread从许多excel文件中导入了它们!但是,如果有更简单的方法导入数据而不重复每个文件的代码序列,那将是一件有趣的事情!)。。我会试试你的解决方案,然后我会更新…@nimrodm谢谢你,伙计。。我想你的代码将帮助我改进很多(并缩短另一个代码-从excel文件导入原始数据-)但是,我明天才能尝试,因为我今天必须做其他事情:(Update1:@klausph第一行我必须修改它以得到我想要的..像这样:Indi_Results{idx,1}=eval('x'))但我不知道如何修改第二行(因此,后面的每一行),因为…如果我照原样做,我会得到以下错误消息:未定义函数“eval”,用于类型为“cell”的输入参数。如果我这样修改它:eval(['x'.\u 5P\u software1'])…我收到了这个错误消息:未定义的函数或变量'x_5P_software1'…(x被识别为一个字母!)请再次帮助我好吗?!好的。这可能是因为x中的字符串在一个单元格中。请尝试将行Indi_All{idx,1}(1,1)
更改为Indi_All{idx,1}{1,1}
。如果不起作用,那么找出x是什么类型并发布在这里。非常感谢您的回复。我再次检查了它,x在一个单元格中…因此我将代码更改为:Indi_Results{idx,2}=prctile(eval([x{1,1}'\u Sim u V_software1']),(5))…它工作得很好…再次感谢