Matlab 如何在regexp之后显示名称?
我正在尝试查找目录中与“hello”匹配的所有文件。我有以下代码:Matlab 如何在regexp之后显示名称?,matlab,Matlab,我正在尝试查找目录中与“hello”匹配的所有文件。我有以下代码: fileData = dir(); m_file_idx = 1; fileNames = {fileData.name}; index = regexp(filenames,'\w*hello\w*','match') ; inFiles = fileNames(~cellfun(@isempty,index)); 如果我的目录中有3个文件,其中包含单词hello,infies将返回我 inFiles =
fileData = dir();
m_file_idx = 1;
fileNames = {fileData.name};
index = regexp(filenames,'\w*hello\w*','match') ;
inFiles = fileNames(~cellfun(@isempty,index));
如果我的目录中有3个文件,其中包含单词hello,infies将返回我
inFiles =
[1x23 char] [1x26 char] [1x25 char]
相反,我希望infles返回文件名,例如thissishello.m、hiandhello.txt
如何以一种简单的方式实现这一点?我认为这里发生的事情是,当单元格数组的一个元素长度超过一定长度(字符串的长度似乎为19个字符)时,matlab不会打印实际的元素,而是打印内容的描述(在本例中为“[1x23 char]”) 例如:
>> names = {'1234567890123456789' 'bar' 'car'}
names =
'1234567890123456789' 'bar' 'car'
>> names = {'12345678901234567890' 'bar' 'car'}
names =
[1x20 char] 'bar' 'car'
celldisp
可能更适合您的情况:
>> celldisp(names)
names{1} =
12345678901234567890
names{2} =
bar
names{3} =
car
我认为这里的情况是,当单元格数组的一个元素长度超过某个长度(字符串的长度似乎为19个字符)时,matlab不会打印实际的元素,而是打印内容的描述(在本例中为“[1x23 char]”) 例如:
>> names = {'1234567890123456789' 'bar' 'car'}
names =
'1234567890123456789' 'bar' 'car'
>> names = {'12345678901234567890' 'bar' 'car'}
names =
[1x20 char] 'bar' 'car'
celldisp
可能更适合您的情况:
>> celldisp(names)
names{1} =
12345678901234567890
names{2} =
bar
names{3} =
car
此代码:
fileData = dir();
fileNames = {fileData.name};
disp('The full directory...')
disp(fileNames)
index = regexp(fileNames,'\w*hello\w*','match');
inFiles = fileNames(~cellfun(@isempty,index));
disp('Print out the file names')
inFiles{:}
生成此输出:
>> script
The full directory...
Columns 1 through 6
'.' '..' 'andsevenyears.txt' 'fourscore.txt' 'hello1.txt' 'hello2.txt'
Column 7
'script.m'
Print out the file names
ans =
hello1.txt
ans =
hello2.txt
在我看来,您在理解单元阵列时似乎遇到了一些问题。M(杰拉德的链接看起来也是一个很好的资源)以下代码:
fileData = dir();
fileNames = {fileData.name};
disp('The full directory...')
disp(fileNames)
index = regexp(fileNames,'\w*hello\w*','match');
inFiles = fileNames(~cellfun(@isempty,index));
disp('Print out the file names')
inFiles{:}
生成此输出:
>> script
The full directory...
Columns 1 through 6
'.' '..' 'andsevenyears.txt' 'fourscore.txt' 'hello1.txt' 'hello2.txt'
Column 7
'script.m'
Print out the file names
ans =
hello1.txt
ans =
hello2.txt
在我看来,您在理解单元阵列时似乎遇到了一些问题。M(jerad的链接看起来也是一个不错的资源)听起来像是
inFiles
是一个单元格数组,可以使用{}索引,例如inFiles{1}
。我建议在帮助手册或教程中学习更多关于matlab的基础知识,例如。听起来像inFiles
是一个单元格数组,可以使用{}索引,例如inFiles{1}
。我建议在帮助手册或教程中学习更多关于matlab的基础知识,例如。