Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

MATLAB: IF function based on a String's name

I have 60 different strings (Book01, Book02, ..., Book60). I want to do a certain procedure only for Book045 until Book58.

How do I write an if statement, so that the procedure is only performed for any String Book045 until Book58? For example:

Book48
    If (name of string = Book045 to Book58)
      My Procedure
    else
      Nothing
    end

Thanks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you have the names in a cell array

books = {'Book01', 'Book02', ..., 'Book59', 'Book60'};

Then you can extract the value in each name and check on that in your loop

for ii = 1:numel(books)
    val = erase( books{ii}, 'Book' ); % Remove the 'Book' prefix
    val = str2double( val );          % Convert to number
    if val >= 45 && val <= 58
        % do something in this range
    end 
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...