How can I read in specific cells which contain file names ending in 'Pressure.mat'?

조회 수: 1 (최근 30일)
I have a list of 490 files in a matrix, I want to only read the files which end in 'Pressure.mat', if it does not end in this I don't want to read it. What function can I use to do this? Even creating a new matrix which only contains these specific cells containing the files would be what I need.

채택된 답변

KSSV
KSSV 2017년 1월 11일
You can run a loop, and use strfind. Find for Pressure.mat in the string, if the output is not empty, then you have to read the file.
  댓글 수: 2
Oliver Bunn
Oliver Bunn 2017년 1월 11일
I have tried this but this only tells me the position of Pressure.mat rather then outputting the entire file name
KSSV
KSSV 2017년 1월 11일
편집: KSSV 2017년 1월 11일
You have filename already in hand in matrix...If it is giving indices of position then you have to pick that matrix(i,j) filename. Else not.
M = [{'coolpressure.mat'} {'cool.mat'} ; {'hello.mat'} {'hipressure.mat'}] ;
for i = 1:2
for j = 1:2
idx = strfind(M{i,j},'pressure.mat') ;
if ~isempty(idx)
fprintf('pick the %s file \n',M{i,j})
end
end
end

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
Stephen23 2017년 1월 11일
편집: Stephen23 2017년 1월 11일
Do not use ugly loops to do this. MATLAB is much more beautiful than that!
Do not use strfind to do this: it will match that string anywhere in the filename, e.g. it will match 'Pressure.mat.txt' or 'Pressure.mat-old', even though these are not what you are looking for.
A much more robust solution is to use regexp to match exactly the string you want:
>> C = {...
'NotThisFile.mat';...
'ThisPressure.mat';...
'YesPressure.mat';...
'NoPressure.mat.txt';...
'WrongPressure.txt';...
};
>> idx = cellfun('isempty',regexp(C,'Pressure\.mat$')); % $ matches end of string!
>> D = C(~idx)
D =
'ThisPressure.mat'
'YesPressure.mat'
You can easily loop over these names:
>> for k = find(~idx)', C{k}, end
ans =
ThisPressure.mat
ans =
YesPressure.mat
Note that regexp is case sensitive. You can use regexpi for a case insensitive match.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by