How can I write a syntax in eval function?

4 ビュー (過去 30 日間)
Wiqas Ahmad
Wiqas Ahmad 2021 年 12 月 17 日
編集済み: Stephen23 2021 年 12 月 19 日
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));
  8 件のコメント
DGM
DGM 2021 年 12 月 19 日
The error message means exactly what it says. The expressions are invalid. This is regardless of the abuse of eval(). They're invalid expressions in any context. I can't reasonably guess which parentheses are missing. You'll have to figure out what the expression is supposed to be.
i = 11; % placeholders
j = 22;
exp1 = ['I_para',num2str(i),num2str(j),'=','(','(sqrt(P12',num2str(i),'./(4.*pi)',').*cos(theta',num2str(j),'.*(cos(phi)).^2)','+','(sqrt(P11',num2str(i),'./(4.*pi)','.*(sin(phi)).^2)',').^2','.* (pi.*(r_med',num2str(i),'.^2.*Qsca_c',num2str(i),')',';']
exp1 = 'I_para1122=((sqrt(P1211./(4.*pi)).*cos(theta22.*(cos(phi)).^2)+(sqrt(P1111./(4.*pi).*(sin(phi)).^2)).^2.* (pi.*(r_med11.^2.*Qsca_c11);'
[nnz(exp1 == '(') nnz(exp1 == ')')]
ans = 1×2
14 11
exp2 = ['I_para',num2str(i),num2str(j),'indx','=','(','(sqrt(P12',num2str(i),'indx','./(4.*pi)',').*cos(theta',num2str(j),'.*(cos(phi)).^2)','+(','(sqrt(P11',num2str(i),'./(4.*pi)','.*(sin(phi)).^2)',').^2','.*(','pi.*(r_med',num2str(i),'.^2.*Qsca_c',num2str(i),')',')',';']
exp2 = 'I_para1122indx=((sqrt(P1211indx./(4.*pi)).*cos(theta22.*(cos(phi)).^2)+((sqrt(P1111./(4.*pi).*(sin(phi)).^2)).^2.*(pi.*(r_med11.^2.*Qsca_c11));'
[nnz(exp2 == '(') nnz(exp2 == ')')]
ans = 1×2
15 12
exp3 = ['I_perp',num2str(i),num2str(j),'indx','=','(','(sqrt(P12',num2str(i),'indx','./(4.*pi)',').*cos(theta',num2str(j),'.*(cos(phi)).^2)','-(','(sqrt(P11',num2str(i),'./(4.*pi)','.*(sin(phi)).^2)',').^2','.*(','pi.*(r_med',num2str(i),'.^2.*Qsca_c',num2str(i),')',')',';']
exp3 = 'I_perp1122indx=((sqrt(P1211indx./(4.*pi)).*cos(theta22.*(cos(phi)).^2)-((sqrt(P1111./(4.*pi).*(sin(phi)).^2)).^2.*(pi.*(r_med11.^2.*Qsca_c11));'
[nnz(exp3 == '(') nnz(exp3 == ')')]
ans = 1×2
15 12
Is there any reason why the constant portions of the expression are split into randomly-sized chunks?
['indx','=','(','(sqrt(P12']
instead of just
['indx=((sqrt(P12']
This seems unnecessary and makes everything more difficult to read.
Mistakes are an eventuality. Unnecessarily complicating things is basically asking for those mistakes to be obfuscated the moment they're made. This is a core element of the warnings already given about eval().
Stephen23
Stephen23 2021 年 12 月 19 日
編集済み: Stephen23 2021 年 12 月 19 日
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.

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Objects についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by