if string is abc return value

3 views (last 30 days)
Jassy
Jassy on 9 Apr 2019
Commented: Jassy on 9 Apr 2019
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 Comments
Adam Danz
Adam Danz on 9 Apr 2019
Edited: Adam Danz on 9 Apr 2019
what if filename has 'a' and 'b' what value does it get?
This question needs to be defined more clearly.
Adam Danz
Adam Danz on 9 Apr 2019
So, it's always the last letter of the filename?

Sign in to comment.

Accepted Answer

Jan
Jan on 9 Apr 2019
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 Comment
Jassy
Jassy on 9 Apr 2019
Thank you so much. It saved me
and next time I will give detail more than this.

Sign in to comment.

More Answers (1)

Adam Danz
Adam Danz on 9 Apr 2019
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

Categories

Find more on Thermal Analysis in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!