0

我正在尝试在与“hello”匹配的目录中查找所有文件。我有以下代码:

fileData = dir();   
m_file_idx = 1;
fileNames = {fileData.name};  
index = regexp(filenames,'\w*hello\w*','match') ;
inFiles = fileNames(~cellfun(@isempty,index));

前任。如果我的目录中有 3 个文件,其中包含单词 hello,inFiles 会返回我

inFiles = 

    [1x23 char]    [1x26 char]    [1x25 char]

相反,我希望 inFiles 向我返回文件名,例如 thisishello.m,hiandhello.txt 我怎样才能以简单的方式做到这一点?

4

2 回答 2

2

这段代码:

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 的链接看起来也是一个很好的资源)

于 2012-12-12T22:14:30.107 回答
1

我认为这里发生的事情是,当单元格数组的元素长于某个长度(字符串似乎是 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
于 2012-12-12T22:09:44.527 回答