Syms in a GUI

4 Ansichten (letzte 30 Tage)
MartinM
MartinM am 11 Dez. 2019
Kommentiert: MartinM am 11 Dez. 2019
Hi,
A simple question. I have an equation that need input parameter. I want this parameter can be choose in a gui befor solving.
But I have some problem when i creat my variable : lamb
Error using assignin
Attempt to add "lamb" to a
static workspace.
See MATLAB Programming,
Restrictions on Assigning to
Variables for details.
Error in syms (line 66)
assignin('caller',x,sym(x));
Error in CodeexeBETA/PushB
(line 130)
syms str2sym(lamb)
Error while evaluating uicontrol Callback
function Simu
close all
f=figure('units','normalized','outerposition',[0 0 1 1]);
%%%%%%%%%%%%%%%%%%%Laser param
dim = [.2 .5 .3 .3];
str = 'Thickness [nm]';
annotation('textbox',dim,'String',str,'FitBoxToText','on','Position',[0.0 0.95 0.075 0.05]);
edit1= uicontrol( 'Style', 'edit','Units', 'normal','Position',[0.0 0.95 0.075 0.02],'FontSize', 15,'BackgroundColor', 'white',...
'String','680');
str2 = 'Radius [µm]';
annotation('textbox',dim,'String',str2,'FitBoxToText','on','Position',[0.075 0.95 0.06 0.05]);
edit2= uicontrol( 'Style', 'edit','Units', 'normal','Position',[0.075 0.95 0.06 0.02],'FontSize', 15,'BackgroundColor', 'white',...
'String','28');
str3 = 'Lambda_{ini} [nm]';
annotation('textbox',dim,'String',str3,'FitBoxToText','on','Position',[0.135 0.95 0.07 0.05]);
edit3= uicontrol( 'Style', 'edit','Units', 'normal','Position',[0.135 0.95 0.07 0.02],'FontSize', 15,'BackgroundColor', 'white',...
'String','800');
str4 = 'Lambda_{end} [nm]';
annotation('textbox',dim,'String',str4,'FitBoxToText','on','Position',[0.205 0.95 0.07 0.05]);
edit4= uicontrol( 'Style', 'edit','Units', 'normal','Position',[0.205 0.95 0.07 0.02],'FontSize', 15,'BackgroundColor', 'white',...
'String','1200');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
str9 = 'Gas';
annotation('textbox',dim,'String',str9,'FitBoxToText','on','Position',[0.545 0.95 0.04 0.05]);
edit9= uicontrol( 'Style', 'edit','Units', 'normal','Position',[0.545 0.95 0.04 0.02],'FontSize', 15,'BackgroundColor', 'white',...
'String','0');
listbox1 = uicontrol('Style','listbox','Units', 'normal','Position',[0.545 0.95 0.04 0.02],'Max',6,'String',{'Air';'Argon';'Xenon'}) ;
str10 = 'P (bar)';
annotation('textbox',dim,'String',str10,'FitBoxToText','on','Position',[0.585 0.95 0.04 0.05]);
edit10= uicontrol( 'Style', 'edit','Units', 'normal','Position',[0.585 0.95 0.04 0.02],'FontSize', 15,'BackgroundColor', 'white',...
'String','1');
str11 = 'Number of point ';
annotation('textbox',dim,'String',str11,'FitBoxToText','on','Position',[0.71 0.95 0.1 0.05]);
edit11= uicontrol( 'Style', 'edit','Units', 'normal','Position',[0.71 0.95 0.1 0.02],'FontSize', 15,'BackgroundColor', 'white',...
'String','200');
PushButton = uicontrol(gcf,'Style', 'push', 'String', 'Run','Units', 'normal','Position',[0.9 0.95 0.05 0.02],'CallBack', @PushB);
PushButton2 = uicontrol(gcf,'Style', 'push', 'String', 'Stop','Units', 'normal','Position',[0.9 0.9 0.05 0.02],'CallBack', @endLoop);
function endLoop(hObject,eventdata,handles)
handles.endLoop=true;
guidata(hObject, handles);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [] = PushB(hObject,eventdata,handles)
% val = str2num(char(get(edit11,'String')));
edi1 = str2num(char(get(edit1,'String')));
edi2 = str2num(char(get(edit2,'string')));
edi3= str2num(char(get(edit3,'string')));
edi4 =str2num(char(get(edit4,'string')));
edi9 =str2num(char(get(edit9,'string')));
edi10 = str2num(char(get(edit10,'string')));
edi11 = str2num(char(get(edit11,'string')));
% gas= (char(get(listbox1,'String')));
index_selected = get(listbox1,'Value');
list = get(listbox1,'String');
gas = list{index_selected}; % Convert from cell array
% to string
gas;
a=2.405*2.405;
c=3e8;
rc=edi2.*1e-6; %en m
t=edi1.*1e-9;% en m
n_g=1.45;
Pression_bar=edi10;
Pression_Pa=Pression_bar*1e5; %%%%% (Pa) %%%%%%%
Pression_atm=101325;
pgas=Pression_bar *1e5;
nbr=edi11;
L_start=edi3 ;
L_stop= edi4 ;
%%%%%%%FOR solving the equation
A1=0.05792105;
A2=238.0185;
B1=0.00167917;
B2=57.362;
C1=0;
C2=0;
syms lamb
ngas= 1 + pgas/101325.* ( (A1 ./ (A2-(lamb.*1e6).^-2) ) + (B1 ./ (B2-(lamb.*1e6).^-2) ) + (C1 ./ (C2-(lamb.*1e6).^-2) )); %ARGON
epsi= n_g.^2 ./ ngas.^2;
k_0= 2*pi./(lamb);
phi = k_0*t* sqrt(n_g.^2-ngas.^2) ;
A=a./( 2.*ngas .*(k_0.*rc).^2);
B=a./ ( ngas.^2 .*(k_0.*rc).^3 );
C = epsi;
D = 0.5 .* (C+1) ./ sqrt(C-1);
nef= ngas - A - B.*D.*cot(phi);
dn = diff(nef,lamb);
dn2 = diff(dn,lamb);
dn3 = diff(dn2,lamb);
beta2=lamb.^3.*dn2./(2*pi*c.^2);
beta3=-lamb.^4./(2*pi*c).^2 .*(3.*dn2./c + lamb.*dn3./c);
% lamb=linspace(800,1200,nbr).*1e-9;
GVD=double(subs(beta2));
TOD=double(subs(beta3));
NEFF=double(subs(neff));
% plot(lamb,GVD)
% figure, plot(lamb,TOD)
end
end
is There an other way than syms?
Regards
Martin

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 11 Dez. 2019
lamb = sym('lamb') ;
Your use of handles suggests that you are using guide. guide cannot manage functions that have matching "end" statements, including not being able to handle nested functions or shared variables.
  1 Kommentar
MartinM
MartinM am 11 Dez. 2019
Perfect.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by