0

I have 60 different character arrays (Book01, Book02, ..., Book60). (For example Book01 is a 1x202040 char.). I want to do a certain procedure only on Book45 until Book58.

How do I write an IF-statement or FOR-loop, so that the procedure is only performed for character arrays Book45 until Book58? For example:

Book05  % Inserted Array for test

if Book45|Book46|Book47|Book48|Book49|Book50|Book51|Book52|Book53|Book54|Book54|Book56|Book57|Book58 % If inserted array is Book45-58     
   % Procedure to be performed on "Inserted Array", only if Book45-58    
else
   % No Procedure on Book01-44 or Book59-60
end

Thanks

3
  • 1
    put all those arrays in a single datastructure, like a struct or a cell array. Then iterating becomes trivial. Commented Jan 9, 2021 at 3:37
  • 2
    is it Book045 or Book45 ? Commented Jan 9, 2021 at 5:30
  • @dpdp. Sharp eye. It should be Book45. I have corrected it in the question. Commented Jan 9, 2021 at 6:18

2 Answers 2

2

as mentioned in the comment, better to put all arrays into one big array. If you insist on calling a particular array you can write:

 for ii=45:58
     a=eval(['Book' num2str(ii)]); % 
     % Procedure to be performed on a
 end

but everywhere I see to try and avoid eval...

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks this works. But I think we might need a IF-Statement, or even this solution coupled with an IF-statement. The reason is, that I need to specify something clearly with an else. If not, even choosing Book05, my procedure is carried out even though it should only be carrying out on Book45-58. Your help is appreciated.
@div that shouldn't be a problem this way. Just iterate over the whole range and use an if- statement based on the index.
@JHBonarius. Thanks but this is exactly what my question is about in the first place, but how to actually do it?
@Div I wrote an answer, but I don't understand "even choosing Book05, my procedure is carried out even though it should only be carrying out on Book45-58" <-- that is a false statement. With the shown code, the procedure should not be called for Book05. If it does, you did something wrong.
0

I'm just putting the comment in an answer, so I can write some code. I actually lost my matlab license yesterday, so cannot test it.

for i = 1:[largest book number]
    book = eval(['Book' num2str(i)]);
    if i >= 45 && i <= 58
        % procedure for book45 until and including book58
    else
        % procedure for other books
    end
end

IMHO the only reason not to use eval is because it is slow (and possibly error prone), but in this case it's not an issue. But instead of having all these separate arrays, you could put all books in a cell array and drop the eval.

edit: but now I read that you have "No Procedure on Book01-44 or Book59-60". If the else statement is empty, then the answer by dpdp is fully sufficient.

2 Comments

If I have a snag somewhere on my side I apologize. Thanks for the help! I still do not know what I am doing wrong? I have made a new question to hopefully be more informative of where my snag might be. Please be so kind to take a look: stackoverflow.com/questions/65643656/…
@Div sorry, I literally lost my license yesterday, so cant test any code. (Switching to python...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.