Matlab 将单元格转换为双精度单元格时发生以下错误:
我正在搜索一个文件夹中的文本文件,并将这些文件添加到一个名为mytext的新数组中 这是我的密码Matlab 将单元格转换为双精度单元格时发生以下错误:,matlab,Matlab,我正在搜索一个文件夹中的文本文件,并将这些文件添加到一个名为mytext的新数组中 这是我的密码 function fileList = removeElements(fileArray) x = 1; mytext = []; for idx = 1:numel(fileArray) if (~isempty(strfind(fileArray(idx),'.txt') )) mytext(x) = fileArray(idx) x=x+1; end en
function fileList = removeElements(fileArray)
x = 1;
mytext = [];
for idx = 1:numel(fileArray)
if (~isempty(strfind(fileArray(idx),'.txt') ))
mytext(x) = fileArray(idx)
x=x+1;
end
end
结束
但是我犯了个错误
??? The following error occurred converting from cell to double:
Error using ==> double
Conversion to double from cell is not possible.
Error in ==> removeElements at 10
mytext(x) = fileArray(idx)
如何克服这个问题?字符串通常存储在单元格数组中。因此,您需要使用
{}
索引,而不是()
:
您可以通过从列表中选择*.txt文件
txtFiles = allFiles(cellfun(@(c)(~isempty(strfind('.txt',c))),allFiles));
如果字符串c包含字符串'.txt',则~isempty(strfind('.txt',c))构造返回true,否则返回false。allFiles(…)构造只返回cellfun构造返回true的单元格数组allFiles的那些元素的单元格数组
txtFiles = allFiles(cellfun(@(c)(~isempty(strfind('.txt',c))),allFiles));