【问题标题】:Store multiple strings in an array in a loop在循环中将多个字符串存储在数组中
【发布时间】:2015-10-02 14:18:42
【问题描述】:

我有几个嵌套循环允许我构建一个 21 行乘 20000 列的矩阵。

上面数组的每一列都有一个关联的字符串,我在每次循环迭代时将其存储在一个名为 filename 的变量中(字符串在每次迭代时被替换)。

有什么可以用字符串创建一个向量 (1-by-20000) 以便我可以导出到 Excel 吗?

clear all

n = 201;


PP=[];
pathname = 'D:\';
addpath(pathname);

for Y = 1:20           
 for B = 1:2          
  for z = 1:50;          
   for R = 1:2;     
    for I = 1:5;

     filename = strcat('F', num2str(Y),'_',num2str(B),'_',num2str(z),'_',num2str(R),'_',num2str(I),'_',num2str(C),'.txt');

     aux = load(filename); 
     PP = [PP aux(1:n)];

   end
  end
 end 
end

rmpath(pathname)

【问题讨论】:

    标签: arrays string matlab for-loop


    【解决方案1】:

    您是否尝试过使用元胞数组:

    filenames = {}; %// Before your loops
    
    filenames(end+1) = {filename}; %// Inside your loops
    

    【讨论】:

    • 这行得通。谢谢。我唯一缺少的是如何将字符串导出到excel。每个字符串一个名称。
    • 你试过xlswrite函数吗?
    • 我试过了,没用。但这是另一个问题。现在好了。非常感谢。我会接受你的回答。
    猜你喜欢
    • 2014-09-02
    • 1970-01-01
    • 2015-08-29
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多