变量值不保存MATLAB

时间:2013-09-25 09:18:08

标签: matlab

我有以下代码:

myFiles = {fullfile(working, 'file1'),fullfile(working, '*.txt'),...
          fullfile(folder1, 'controllerX'))};

switch (test)
  case 'test1'

       myFiles = {fullfile(working, folder1, '*.txt'),...
                 fullfile(working, folder1,folder2,'test.xml')};

 case 'test2'
          myFiles = {fullfile(working, folderX, '*.txt'),...
                 fullfile(working, folder1,folder2,'test.xml')}

end
for m = 1:numel(myFiles )
    file = dir(myFiles{m});
    verify(numel(file) == 1)
end

问题是在循环中只对case1或2执行检查,即:numel(myFiles)=在case1或case 2中找到的文件数,所以第一次将值赋给myFiles时,值不会被保存....我希望myFiles =第一次的文件数和案例1或案例2中的数字

1 个答案:

答案 0 :(得分:1)

根据您的问题和评论,我认为您希望将不同的文件(案例test1,案例test2)合并到您的初始单元格字符串数组中。

由于myFiles是一个行数组,你可以像这样连接它们:

switch(test)
case 'test1'
    myFiles = [myFiles, {fullfile..., fullfile...}];
case 'test2'
    myFiles = [myFiles, {fullfile..., fullfile...}];
end

如果稍后,您无法保证单元格数组的大小布局,而是使用union,在这种情况下会过滤掉双条目:

switch(test)
case 'test1'
    myFiles = union(myFiles, {fullfile..., fullfile...});
case 'test2'
    myFiles = union(myFiles, {fullfile..., fullfile...});
end