How can I write a syntax in eval function?

1 view (last 30 days)
Wiqas Ahmad
Wiqas Ahmad on 17 Dec 2021
Edited: Stephen23 on 19 Dec 2021
Is it possible to write the following syntax in eval function? If yes, please write it down
I_para(i,:)=(((((sqrt(P12(i,indx)./(4.*pi))).*cos(theta(j)).*(cos(phi)).^2)) + ((sqrt(P11(i,indx)./(4.*pi))).*((sin(phi)).^2))).^2).* (pi.*(r_med(i)).^2.*Qsca_c(i));
Stephen23 on 19 Dec 2021
Edited: Stephen23 on 19 Dec 2021
As DGM explains, by using EVAL you force yourself into writing unnecessarily complex code. But the disadvantages are not only that your code is more complex and obfuscated (than if you used basic indexing), but that you also remove all of MATLAB's inbuilt code helping tools (e.g. mlint static code checking, syntax error highlighting, variable highlighting, code warnings, etc.) which would help you to debug errors. So your design decision makes your code harder to debug. Your question is a good example of this.
Dynamically accessing variable names is also slow and inefficient compared to using basic MATLAB indexing.

Sign in to comment.

Answers (0)


Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!