Evaluate if pushbutton is pressed in conditional 'if'

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

 Accepted Answer

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

I'm still having a problem
Error using Results_after_opt>callback_mors
Too many input arguments.
Error while evaluating UIControl Callback
I changed as you show me. I getting the data from a third window, so with the next function I'm trying to gather the data but the problem seems to be when I press the pushbutton.
function callback(hObject)
res_after_opt = guidata(hObject);
hfig_res_opt= findobj('Tag','reswopt');
res_opt=guidata(hfig_res_opt);
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
I'm just want to display either a mesh or a surf as a new figure from data that is stored in a different figure.
I think callback functions need at least 2 input arguments (hObject and event). Try:
function callback(hObject,~)
%code
end
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)
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.
I found the problem about the last question. Thank you Jan and Dennis for the help.

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks 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!