2

My code has 2 parts. First part is an automatic file opening programmed like this :

fichierref = 'H:\MATLAB\Archive_08112012';
files = dir(fullfile(fichierref, '*.txt'));
numberOfFiles = numel(files);
delimiterIn = ' ';
headerlinesIn = 11;
for d = 1:numberOfFiles
    filenames(d) = cellstr(files(d).name);
end

for i=1:numberOfFiles
    data = importdata(fullfile(fichierref,filenames{i}),delimiterIn,headerlinesIn);
end

Later on, I want the user to select his files for analysis. There's a problem with this though. I typed the lines as follow :

reference = warndlg('Choose the files from which you want to know the magnetic field');
uiwait(reference);
filenames = uigetfile('./*.txt','MultiSelect', 'on');
numberOfFiles = numel(filenames);
delimiterIn = ' ';
headerlinesIn = 11;

It's giving me the following error, after I press OK on the prompt:

Cell contents reference from a non-cell array object.

Error in FreqVSChampB_no_spec (line 149)
data=importdata(filenames{1},delimiterIn,headerlinesIn);

I didn't get the chance to select any text document. Anyone has an idea why it's doing that?

4

1 回答 1

1

uigetfile与“MultiSelect”一起使用时有点烦人:当您选择多个文件时,输出将作为(字符串)元胞数组返回。但是,当只选择一个文件时,输出是字符串类型(不是像预期的那样具有单个单元格的单元格数组)。

所以,为了解决这个问题:

filenames = uigetfile('./*.txt','MultiSelect', 'on');
if ~iscell(filenames) && ischar( a )
    filenames = {filenames}; % force it to be a cell array of strings
end
% continue your code here treating filenames as cell array of strings.

编辑:
正如@Sam 所指出的,必须验证用户没有在 UI 上按下“取消”(通过检查这filenames是一个字符串)。

于 2013-04-24T14:26:59.563 回答