if string is abc return value

조회 수: 1 (최근 30일)
Jassy
Jassy 2019년 4월 9일
댓글: Jassy 2019년 4월 9일
Hi.
I have filename is string and then I want to find specific name.
if filename have ' a ' xxx = 1
if filename have ' b ' xxx = 2
if filename have ' c ' xxx = 3
thank you.
  댓글 수: 7
Adam Danz
Adam Danz 2019년 4월 9일
편집: Adam Danz 2019년 4월 9일
what if filename has 'a' and 'b' what value does it get?
This question needs to be defined more clearly.
Adam Danz
Adam Danz 2019년 4월 9일
So, it's always the last letter of the filename?

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

채택된 답변

Jan
Jan 2019년 4월 9일
Maybe:
[fPath, fName, fExt] = fileparts(filename);
if endsWith(fName, 'a ') % With the space as in your example
xxx = 1;
elseif endsWith(fName, 'b ')
xxx = 2;
... etc
end
Or:
[fPath, fName, fExt] = fileparts(filename);
switch fname(end-1) % Again assuming you mean the 2nd last character
case 'a'
xxx = 1;
case 'b'
xxx = 2
... etc
otherwise
error('Unexpected charatcer')
end
  댓글 수: 1
Jassy
Jassy 2019년 4월 9일
Thank you so much. It saved me
and next time I will give detail more than this.

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

추가 답변 (1개)

Adam Danz
Adam Danz 2019년 4월 9일
The cell array 'key' lists all possible last-characters and the order determines the value.
filename = 'Z2q0002b.jpg';
[~, fName] = fileparts(filename);
key = {'a' 'b' 'c'};
xxx = find(strcmp(key, fName(end)));
xxx =
2

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by