You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How can I write a syntax in eval function?
1 view (last 30 days)
Show older comments
Wiqas Ahmad
on 17 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));
8 Comments
John D'Errico
on 17 Dec 2021
You want to use eval on that? Please don't. There can be no good reason to do so.
Wiqas Ahmad
on 17 Dec 2021
Using eval command, I can better interpret my program output and will be understood that's why. I tried some syntax to convert but shows the following error.
eval(['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),')',';']);
Error using polar_2 (line 25)
Error: Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched
delimiters.
Stephen23
on 17 Dec 2021
Edited: Stephen23
on 17 Dec 2021
"Using eval command, I can better interpret my program output and will be understood that's why."
In reality it will make it much more complex (as the mere existence of this question should be warning you).
"I tried some syntax to convert but shows the following error."
Perhaps the difficulty of debugging one simple line of code gives you one clue why that approach should be avoided:
Using EVAL forces you into writing slow, complex, inefficient, buggy code that is hard to debug.
The simple and efficient approach is to use indexing, just like MATLAB is designed for.
Wiqas Ahmad
on 17 Dec 2021
Edited: Wiqas Ahmad
on 17 Dec 2021
clc;
clear all;
close all;
%% ------------------------------Program-------------------------------------
N0=100;
r_med=[0.445 0.889 1.445 8];
sigma_g=7;
N_ang=91;
theta=[0 20]/180*pi;
t = [0:180]/180*pi;
phi =[0 30]/180*pi;
Num_r = 50e3;
r = linspace(1,50,Num_r)./2;
col=['k' 'b' 'r' 'g'];
for i=1:length(r_med)
hold on
for j=length(theta)
[P11(i,:),P12(i,:),P33(i,:),P34(i,:),Qsca_c(i,:),~,~] = ZK_W_Cloud_PhaseFunc(N0,r_med(i),sigma_g,N_ang);
[~,indx] = find(theta(j)==t);
eval(['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),')',')',';']);
eval(['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),')',')',';']);
%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));
%I_perp(i,:)=((((sqrt(P12(i,indx)./(4.*pi))).*cos(theta(j)).*cos(phi).*sin(phi))-((sqrt(P11(i,indx)./(4.*pi))).*sin(phi).*(cos(phi)))).^2).*(pi.*(r_med(i)).^2.*Qsca_c(i));
p1=eval(['plot(I_para',num2str(i),num2str(j),',phi)',';']);
p1=eval(['plot(I_perp',num2str(i),num2str(j),',phi)',';']);
p1.LineWidth=1;
%plot(phi,I_para(i,j),'color',col(i),'Linewidth',1.5);
%plot(phi.*180/pi,I_perp(i,j),'color',col(i),'Linewidth',1.5);
end
end
%%
figure(1)
subplot(1,2,1);
hold on
plot(phi,I_para11,'color',col(1),'Linewidth',1.5);
plot(phi,I_para21,'color',col(2),'Linewidth',1.5);
plot(phi,I_para31,'color',col(3),'Linewidth',1.5);
plot(phi,I_para41,'color',col(4),'Linewidth',1.5);
%title('\bf (a) Scattering phase function','FontSize',...
%16,'FontWeight','normal')%\bf(bold font),\rm(normal font),\it(italian font)
leg=legend('R_{eff}= 4\mum','R_{eff}= 8\mum','R_{eff}= 13\mum','R_{eff}= 18\mum','location','Northeast',...
'orientation','vertical','Fontsize',6.5);% we need to calculate Reff from Rm using formaula
legend boxon
leg.ItemTokenSize = [15,18];
xlabel('\phi(\circ)');
ylabel('I_{||}');
set(gca,'color','w','Fontsize',12,'LineWidth',1,'Fontweight','normal');
set(gca,'box','on','Fontname','Arial','Fontsmoothing','on');
set(gca,'xlim',[0 90],'xtick',[0:30:90],'ylim',[0 6].*10.^-2,'ytick',[0:2:6].*10.^-2);
set(gca,'xgrid','on','ygrid','on','gridcolor','k');
%%
subplot(1,2,2);
hold on
plot(phi,I_perp11,'color',col(1),'Linewidth',1.5);
plot(phi,I_perp21,'color',col(2),'Linewidth',1.5);
plot(phi,I_perp31,'color',col(3),'Linewidth',1.5);
plot(phi,I_perp41,'color',col(4),'Linewidth',1.5);
%title('\bf (a) Scattering phase function','FontSize',...
%16,'FontWeight','normal')%\bf(bold font),\rm(normal font),\it(italian font)
leg=legend('R_{eff}= 4\mum','R_{eff}= 8\mum','R_{eff}= 13\mum','R_{eff}= 18\mum','location','Northeast',...
'orientation','vertical','Fontsize',6.5);% we need to calculate Reff from Rm using formaula
legend boxon
leg.ItemTokenSize = [15,18];
xlabel('\phi(\circ)');
ylabel('I_{\perp}');
set(gca,'color','w','Fontsize',12,'LineWidth',1,'Fontweight','normal');
set(gca,'box','on','Fontname','Arial','Fontsmoothing','on');
set(gca,'xlim',[0 90],'xtick',[0:30:90],'ylim',[0 12].*10.^-3,'ytick',[0:3:12].*10.^-3);
set(gca,'xgrid','on','ygrid','on','gridcolor','k');
%%
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[.3 .3 .45 .38]);
%print(gcf,'figure.tiff','-dtiff','-r300');
This is my whole program. I want to change the index i while keep the index j constant in the plot command (plot(phi,I_para11,'color',col(1),'Linewidth',1.5);) . Previously, I had done the similar plotting using eval commnad. Now I'm trying to use it again but the structure of my program is quite different and I can't use it now. I tried but facing some errors i don't know.@Rik
Sargondjani
on 18 Dec 2021
Others were suggesting to use indexing. For example, with a cell array:
I_para{1,1} = ....
I_para{1,2} = ...
DGM
on 19 Dec 2021
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
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.
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)