MATLAB Answers

Evaluate if pushbutton is pressed in conditional 'if'

38 views (last 30 days)
Oscar Espinosa
Oscar Espinosa on 14 Jun 2018
Commented: Oscar Espinosa on 21 Jun 2018
Hi everybody, Im trying to evaluate if a pushbutton is pressed and then plot my data in a UI. Here is the code:
function Results_after_opt()
%Results_after_opt is function that shows the main values obteined after
%the optimization and aproximation of the results
%
%Creation of the figure
fig = figure('units','pixels',...
'position',[10 850 265 155],...
'menubar','none',...
'name','Расчет АХ самолетов - Меню',...
'numbertitle','off',...
'resize','off');
res_after_opt = guihandles(fig);
%Figure in the center
scrsz = get(0, 'ScreenSize');
pos_act=get(gcf,'Position');
xr=scrsz(3) - pos_act(3);
xp=round(xr/2);
yr=scrsz(4) - pos_act(4);
yp=round(yr/2);
set(gcf,'Position',[xp yp pos_act(3) pos_act(4)]);
%---------------------------------------
%
hfig_res_opt= findobj('Tag','reswopt');
res_opt=guidata(hfig_res_opt);
%
%Components
res_after_opt.smin_m0 = uicontrol('Style','text', ...
'Units','pixels', ...
'Position',[5 95 150 25], ...
'String','min m0');
%
res_after_opt.min_m0=uicontrol('Style','text', ...
'Units','pixels', ...
'Position',[160 95 100 25], ...
'String',res_opt.min_m0);
%
res_after_opt.slz_opt_m0=uicontrol('Style','text', ...
'Units','pixels', ...
'Position',[5 65 150 25], ...
'String','lz opt m0');
%
res_after_opt.lz_opt_m0=uicontrol('Style','text', ...
'Units','pixels', ...
'Position',[160 65 100 25], ...
'String',res_opt.lz_opt_m0);
%
res_after_opt.snu_opt_m0=uicontrol('Style','text', ...
'Units','pixels', ...
'Position',[5 35 150 25], ...
'String','nu opt m0');
%
res_after_opt.nu_opt_m0=uicontrol('Style','text', ...
'Units','pixels', ...
'Position',[160 35 100 25], ...
'String',res_opt.nu_opt_m0);
%
res_after_opt.pb_surf=uicontrol('Style','pushbutton', ...
'Units','pixels', ...
'Position',[5 5 125 25], ...
'String','Surf b effective',...
'callback',@callback);
res_after_opt.pb_mesh=uicontrol('Style','pushbutton', ...
'Units','pixels', ...
'Position',[135 5 125 25], ...
'String','Mesh b effective',...
'callback',@callback);
guidata(fig,res_after_opt);
end
And the callback function.
function callback(hObject)
res_after_opt = guidata(hObject);
if res_after_opt.pb_surf==1
surf(res_opt.topl_ef(:,:));
elseif res_after_opt.pb_mesh==1
mesh(res_opt.topl_ef(:,:));
end
end

  0 Comments

Sign in to comment.

Accepted Answer

Jan
Jan on 15 Jun 2018
I guess you mean the Value of the objects:
function callback(hObject)
res_after_opt = guidata(hObject);
if res_after_opt.pb_surf.Value == 1
surf(res_opt.topl_ef);
elseif res_after_opt.pb_mesh.Value == 1
mesh(res_opt.topl_ef);
end
end
By the way, omit the "(:,:)" in x(:,:), because it wastes time only. x is sufficient and clear already.

  5 Comments

Show 2 older comments
Jan
Jan on 21 Jun 2018
Dennis is right: All callbacks are called with 2 inputs. If you are working with GUIDE you need 3 inputs:
function callback(hObject, EventData, handles)
Oscar Espinosa
Oscar Espinosa on 21 Jun 2018
I already did the changes, and a new trouble appears
Error while evaluating UIControl Callback
Error using mesh (line 83)
Z must be a matrix, not a scalar or vector.
Error in Results_after_opt>callback (line 122)
mesh(res_opt.topl_ef);
Error while evaluating UIControl Callback
How should I write the code correctly? When I plotted the data from the workspace (no throught UI), I used either the mesh or the surf button located in the instrument panel and everything ran smoothly.
Oscar Espinosa
Oscar Espinosa on 21 Jun 2018
I found the problem about the last question. Thank you Jan and Dennis for the help.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!