非细胞阵列在Matlab uigetfile [英] Non-cell array with uigetfile in Matlab
问题描述
我的code有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;
它给我下面的错误后,我preSS上的提示OK:
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?
推荐答案
uigetfile
是一个有点恼人与'多选'用时:当您选择多个文件输出被返回作为单元阵列(串)。 然而,当时只有一个文件选择输出类型的字符串(不可以单元阵列与一个单细胞,正如人们所预料)。
uigetfile
is a bit of an annoying when used with `MultiSelect': when you select multiple files the output is returned as a cell array (of strings). However, when only one file is selected the output is of type string (not a cell array with a single cell, as one would have expected).
所以,为了解决这个问题:
So, in order to fix this:
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上preSS取消(通过检查文件名
是一个字符串)。
这篇关于非细胞阵列在Matlab uigetfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!