Need to find files in a dir structure with similar but not identical names

33 ビュー (過去 30 日間)
Alex
Alex 2014 年 7 月 28 日
コメント済み: Image Analyst 2014 年 7 月 30 日
I am trying to pull data from multiple files in a directory. I have written a for loop to get to the specified file, but when I search for the file using an * is cannot be found. For example, the file name 072814_Nominal.xls and another file in the same directory might be called 081213_Nominal.xls. I want to pull date from only files with "Nominal" in the name. I have tried using the below loop
D=dir(pathname);
for i=1:length(D)
if D(i).name=='*Nominal.xls' %this line is trouble - says matrix dimensions must agree
filename=fullfile(pathname,D(i).name);
temp(i)=xlsread(filename,'Sheet1','R4');
else
end
end
but I am getting the "Error using ==", "matrix dimensions must agree" error and I cannot find a way to make Matlab find the file that looks like 'Nominal' or 'Nominal.xls' (I have tried both). Please help!
  1 件のコメント
per isakson
per isakson 2014 年 7 月 28 日
編集済み: per isakson 2014 年 7 月 28 日
  • See regular expressions or
D = dir( '*Nominal.xls' );
' Matlab don't support anything close to D(i).name=='*Nominal.xls'

サインインしてコメントする。

採用された回答

Ahmet Cecen
Ahmet Cecen 2014 年 7 月 29 日
Look for an implementation of grep or agrep for MATLAB. However if your problem has a monotonic redundancy, I might be able to suggest an alternative. For example, if you are only concerned about files ENDING with a very specific name (without possibility of spelling errors) such as '_nominal.xls', then here is a very lazy way to do it.
files = dir('*.xls');
for k = 1:length(files)
current=files(k).name;
check(k)=strcmp(current(end-10:end),'nominal.xls');
end
list=find(check);
images = cell(1, length(list));
for k = 1:length(list)
data{k}=AN_APPROPRIATE_LOAD_FUNCTION_FOR_XLS_WITH_YOUR_PARAMETERS(files(list(k)).name);
end
Now only files whose name has the last 11 characters exactly 'nominal.xls' is loaded into the cell data.

その他の回答 (3 件)

Image Analyst
Image Analyst 2014 年 7 月 29 日
Don't use
D(i).name=='*Nominal.xls'
use strfind() instead
if ~isempty(strfind(D(i).name, 'Nominal.xls')) % Don't use *Nominal use Nominal
  4 件のコメント
per isakson
per isakson 2014 年 7 月 29 日
編集済み: per isakson 2014 年 7 月 29 日
@Alex, try this
filename = '081213_Nominal.xls';
pos = strfind( filename, 'Nominal.xls' )
isf = not( isempty( strfind( filename, 'Nominal.xls' ) ) )
it returns
pos =
8
isf =
1
Image Analyst
Image Analyst 2014 年 7 月 30 日
I agree with the others. strfind would have worked if you had just tried it, and as per just proved. The fact that a time stamp is somewhere in the filename does not matter at all - not sure why you thought it would unless you just didn't even read the documentation for the strfind function.

サインインしてコメントする。


per isakson
per isakson 2014 年 7 月 29 日
An alternative
D = dir( fullfile( pathname, '*Nominal.xls' ) );
for jj = 1 : length(D)
filespec = fullfile( pathname, D(jj).name );
temp(jj) = xlsread( filespec, 'Sheet1','R4');
end

John
John 2014 年 7 月 29 日
Hi. I would like to add one piece of advice. In MATLAB, string comparisons for the purposes of logical testing are best done with 'strcmp'. You likely got that matrix dimensions error because strings in MATLAB are character arrays and if the filenames have different lengths then so will the character vectors that represent them. Strcmp, strfind, etc... are the way to go for string comparisons.

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by