【问题标题】:Construct a variable name from two strings values in a loop function从循环函数中的两个字符串值构造变量名
【发布时间】:2013-01-18 16:15:07
【问题描述】:

我是 MATLAB 的初学者,我希望我能在这个不错的网站上找到帮助(再次 :))。 它是关于一系列个体的模型预测结果。因此,我有一系列 变量(包含每个个体的基本结果)及其命名法不同 仅在第一部分..e,g:

Mike 结果保存为:Mike_Sim_V_software1 亚当结果另存为:Adam_Sim_V_sofwtare1 莎拉结果另存为:Sarah_Sim_V_sofwtar1 等等……

我已经有了名字列表 ['Mike','Adam','Sarah', etc.] 和 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 包含一个字符串值,如:'迈克'..

我的问题是:

1) 我可以解决这个问题吗?是否有任何函数可以从两个不同的字符串构造变量的名称?!所以也许类似于 num2str 的东西,但要在字符串中添加字符串! 换句话说,将变化部分'Mike','Adam'等添加到不变部分'_Sim_V_software1'以获得我可以在循环函数中使用的变量?!

我不知道...我觉得我的问题很愚蠢,但不知怎的,我为此花了很多时间,但它并没有按照我想要的方式工作! 希望这与我的大脑已经知道周末即将开始的事实无关:)

很高兴收到您的来信,并在此先感谢您...!

【问题讨论】:

    标签: string matlab variables loops


    【解决方案1】:

    其实很简单。您需要像这样使用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],如上所示。

    问候

    【讨论】:

    • Update1: @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 是什么类型并将其发布在这里。
    • 非常感谢您的回复..我再次检查了它,x 在一个单元格中...所以我将代码更改为: Indi_Results {idx,2} = prctile (eval([x{ 1,1} '_Sim_V_software1'])', (5)) ...而且效果很好...再次感谢
    【解决方案2】:

    使用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...:您的数据集是来自具有上述名称的文件,还是它们是变量(可能由其他脚本生成)?
    • 伙计们,非常感谢你的回答,很抱歉迟到了......前面提到的数据已经保存为变量......(我已经使用从许多 excel 文件中导入了它们xlsread!但是,如果有更简单的方法来导入数据而无需为每个文件重复代码序列,将会很有趣!)..我会尝试你的解决方案,然后我会更新......跨度>
    • @nimrodm 非常感谢。明天再试试,因为我今天必须做其他事情:(
    猜你喜欢
    • 1970-01-01
    • 2014-12-02
    • 2019-09-30
    • 2021-01-15
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    • 2020-05-22
    • 2020-05-10
    相关资源
    最近更新 更多