필터 지우기
필터 지우기

How to search for specific characters within a string.

조회 수: 2 (최근 30일)
Alex
Alex 2013년 9월 18일
댓글: dpb 2013년 10월 1일
Say I have the following files,
foo1.85e-01.mat
foo1.79e+00.mat
foo1.82e+00.mat
foo1.94e+00.mat
foo2.30e+00.mat
How can I select just the files with numbers between 1.8 and 2.0, i.e., the third and fourth files? Together the commands
flist = dir('foo1.8*e+00.mat')
and
flist = dir('foo1.9*e+00.mat')
return all the desired files. But is there a single query that will return the same files?

채택된 답변

dpb
dpb 2013년 9월 18일
The OS wildcard search isn't flexible enough directly; I'd just return them all and then winnow the list while processing it -- sotoo
d=dir('foo*.mat');
for ix=1:length(d)
fn=d(ix).name;
if ~iswithin(sscanf(fn,'foo%f.mat'), 1.8, 2.0), break, end % skip unwanted
...
end
where iswithin is my handy helper function
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
  댓글 수: 2
Alex
Alex 2013년 10월 1일
Thanks, dpb. I've implemented something similar in my code now. The sscanf function was the thing I was looking for, something that would extract numbers from a string.
dpb
dpb 2013년 10월 1일
Glad it helped...

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by