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

Categories

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.


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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...