How do i use eval function when the sentence has apostrophe (') in it?

조회 수: 20 (최근 30일)
Hoon
Hoon 2014년 2월 27일
댓글: Steven Lord 2019년 10월 28일
I'm trying to use eval function with for loop
B1=sinc(x).*sinc(y);
B2=sinc(2x).*sinc(2y);
B3=sinc(3x).*sinc(3y);
B4=sinc(4x).*sinc(4y);
B5=sinc(5x).*sinc(5y);
for i=1:5
s=['imwrite(B' num2str(i) ','B_' num2str(i) '.bmp')']
eval(s);
end
But this sentence does not make sense since in some elements in "s" there are 3 apostrophe,
which is impossible for computer to compute.
Also, imwrite function got to have apostrophe for file name....
how do i make a breakthrough?
  댓글 수: 2
Stephen23
Stephen23 2017년 12월 11일
편집: Stephen23 2017년 12월 11일
Ugh. Do NOT use eval for such trivial code.
See Jos's answer below for the best way to do this (best in the sense faster, neater, less buggy, more efficient, easier to debug, more secure).

댓글을 달려면 로그인하십시오.

채택된 답변

Jos (10584)
Jos (10584) 2014년 2월 27일
An apostrophe, or rather a single quote as it is called, in a string can be accomplished by doubling it, like in:
str = 'it''s me!'
More importantly, you do not want to use eval at all, by designing your variables a little bit more clever using, e.g., cell arrays or structs.
B(1).values = sinc(x).*sinc(y);
B(2).values = sinc(2x).*sinc(2y); % note that 2x is invalid syntax, but I do no know what you mean!
% B(3).values = . . . etcetera
for k = 1:numel(B)
filename = sprintf('B_%d.bmp',k)
imwrite(B(k).values, filename)
end
This away you avoid all the complications induced by eval ...
  댓글 수: 2
Hoon
Hoon 2014년 2월 28일
God, both answers are great! How can i choose both?
Jacob Halbrooks
Jacob Halbrooks 2014년 2월 28일
+ 1 from me for Jos' answer. I agree that EVAL should be avoided when possible

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Jos (10584)
Jos (10584) 2014년 2월 28일
편집: Jos (10584) 2014년 2월 28일
This is one of the reasons you should not use eval:
eval(char('fkur*)Ykvj"GXCN"{qw"pgxgt"mpqy"yjcv"jcrrgpu0"Kv"eqwnf"jcxg"hqtocvvgf"{qwt"jctfftkxg"000)+'-2)) ;
  댓글 수: 2
Derrick Lim
Derrick Lim 2019년 10월 28일
편집: Derrick Lim 2019년 10월 28일
Out of curiosity, what exactly does that command do? (Too scared to actually try it out)
Steven Lord
Steven Lord 2019년 10월 28일
Just run the part of that command starting with "char(" and ending with the closing parenthesis for the char function call. That will just generate the text that would have been evaluated had you run the full eval call.

댓글을 달려면 로그인하십시오.


Jacob Halbrooks
Jacob Halbrooks 2014년 2월 27일
편집: Jacob Halbrooks 2014년 2월 27일
If you need EVAL, I'd suggest using SPRINTF to form your expression. This lets you see the command you are writing more naturally and makes apostrophe management a little easier:
for i=1:5
s = sprintf('imwrite(%s, ''%s'')', ...
['B' num2str(i)], ['B_' num2str(i) '.bmp']);
eval(s);
end
  댓글 수: 3
Hoon
Hoon 2014년 2월 28일
God, both answers are great! How can i choose both?
Stephen23
Stephen23 2017년 12월 11일
@Hoon: use Jos's answer. It is the better way of solving this problem.

댓글을 달려면 로그인하십시오.


Iain
Iain 2014년 2월 28일
Heres a third answer....
apostrophe = char(39);
then just use apostrophe
And a fourth answer:
If you actually want an apostrophe, and not a string start/termination character, you can use other apostrophes.
E.g, Alt+0145 = ‘ and Alt+0146 = ’

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by