Matlab:附加单元格数组

时间:2014-06-03 19:58:50

标签: matlab

我有10个二进制文件,每个文件都存储一个数字列表。我想依次加载每个文件,然后在该文件中附加一个单元格数组y。因此,如果每个文件包含20个数字,我希望我的最终单元格为10x20。我该怎么做呢?以下代码不起作用:

for i=1:10
    % Load an array into variable 'x'
    y = {y x}
end

2 个答案:

答案 0 :(得分:1)

如果你正在读严格的数字并想要一个数组(而不是单元格),这可能有效:

% read CSV numbers from file into array
temp = {};
out = [];
for i=1:10
   % my example files were called input1.txt, input2.txt, etc
   filename = strcat('input', num2str(i), '.txt');
   fid = fopen(filename, 'r');
   temp = textscan(fid,'%d','delimiter',',');      
   out(i,:) = cell2mat(temp);
   fclose(fid);
end

'出'是一个10x20阵列

答案 1 :(得分:1)

您只需要对代码进行一些小修改:

y = cell(1,10); %// initiallize if possible. Not necessary
for ii = 1:10 %// better not use i as a variable (would override imaginary unit)
    %// Load an array into variable 'x'
    y{ii} = x; %// fill ii-th cell with x.
    %// Or use y{end+1} = x if you haven't initiallized y        
end