Problem with quotation marks (')

I'm helping a friend of mine do a job in matlab and he's using a piece of code that looks like this:
......
comando= ['IM_' num2str(i) '_0' num2str(j) '_' num2str(k) '=leimagens(' num2str(i) '_0' num2str(j) '_' num2str(k) '.png); ' ]
eval(comando);
........
is showing an error, I could see that when the code is executed without the eval command, it executes the statement as follows: IM_9_11_3 = leimagens (9_11_3.png); but the correct one would be (the form I need is:) IM_9_11_3 = leimagens ('9_11_3.png'); the doubt is, how to insert the '(quotation marks) in the text? the problem is that when the '' is inserted into the code before the eval command, it is not recognized
Thanks a lot for the help!

1 Comment

Stephen23
Stephen23 on 19 Jan 2019
Edited: Stephen23 on 19 Jan 2019
Ugh.
This is one reason why using eval is such a bad way to write code: really basic things (like accessing your data) turn into complex buggy nightmares that are difficult to debug. Using eval means that none of the code checking tools work, which means that MATLAB cannot show you the exact location of the problem. So this is another example of how the beginners who decide to dynamically access variable names force themselves into wasting lots of time (run-time, debugging time, and waiting-for-others-to-help-them time) by writing slow, complex, buggy code that is hard to debug. Read this to know why:
The best solution is to turn those ugly pseudo-indices into real indices. Indexing is neat, simple, easy to debug, and very efficient. Unlike what you are trying to do.
"he's using a piece of code that looks like this:"
Well, clearly he is not using a piece of code like that. He might have written a piece of code like that, but it won't work so he can't use it. He could use it if he fixes that badly written code, which could be done in atleast two ways:
  1. mucking around with single-quotes until works, by asking you and (indirectly) random strangers on the internet to help figure out how to make it work... but the code will still be slow and badly designed.
  2. rewriting the code so that it uses indexing (much simpler, much neater, much more efficient, much easier to debug (you would probably be able to debug it yourselves!)).
If you insist on writing slow, complex code that inhibits the MATLAB JIT engine from speeding up your code and from helping you to write bug-free code, then you should use sprintf: it will avoid all of those num2str calls and make it clearer where you need to add pairs of single-quotes.

Sign in to comment.

 Accepted Answer

madhan ravi
madhan ravi on 19 Jan 2019
Edited: madhan ravi on 19 Jan 2019

2 Comments

Stephen23
Stephen23 on 19 Jan 2019
Edited: Stephen23 on 19 Jan 2019
"Eval is evil "
That makes me laugh, because there are over three hundred discussions using this term:
and over one million hits when I use [major internet search engine] to search for "evil eval".
Nice term.
Haha indeed .

Sign in to comment.

More Answers (1)

Categories

Community Treasure Hunt

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

Start Hunting!