Problem with arguments in function call - button

I'm creating a tabbed interface, so I can not use the GUI. I need to generate a chart, but it does not read my data in the inbox.
uicontrol('Parent', TabHandles{1,1}, ...
'Units', 'pixels', ...
'Position', [ round(PanelWidth/6.5) 6.5*ButtonHeight ...
round(PanelWidth/13) round(PanelHeight/18) ],...
'String', 'Ver', ...
'Callback', @VerCallback, ...
'Style', 'pushbutton',...
'HorizontalAlignment', 'center',...
'FontName', 'arial',...
'FontWeight', 'bold',...
'FontSize', 10);
function VerCallback(haxes2,~)
%a = get(r,'str2num');
%b = get(x,'str2num');
%plot (haxes2, 1:10, [0 a], [0 b]);
%plot(haxes2, [0 2], [0 3]);
cla;
end

Antworten (1)

Adam
Adam am 6 Feb. 2017
Bearbeitet: Adam am 6 Feb. 2017

0 Stimmen

When you use this syntax for a callback
'Callback', @VerCallback
The callback function will have exactly 2 input arguments, representing the source (in this case the push button handle) and some event data which is often of little use).
If you want to pass further arguments, such as an axes handle you need something more like:
'Callback', @(src,evt) VerCallback( hAxes2 )
assuming hAxes2 is defined at the point you create your uicontrol (or rather when you set its callback, which in your code is the same thing). There are other syntaxes too, but this is the one I use.
If you do want either of the source or event data arguments in your function then you can also pass these in:
'Callback', @(src,evt) VerCallback( src, evt, hAxes2 )

2 Kommentare

My button would have to plot a line [0 r] [0 x], but it does not.
haxes2 = axes('Parent', TabHandles{1,1}, ...
'Units', 'pixels', ...
'Position', [37*PlotOffset 7*PlotOffset ...
PanelWidth-40*PlotOffset PanelHeight-10*PlotOffset]);
r = uicontrol('Parent', TabHandles{1,1}, ...
'Units', 'pixels', ...
'Position', [ round(PanelWidth/5) 8.5*ButtonHeight ...
round(PanelWidth/10) round(PanelHeight/18) ],...
'Parent', TabHandles{1,1}, ...
'String', 'R [Ohm]', ...
'Style', 'edit',...
'HorizontalAlignment', 'center',...
'FontName', 'arial',...
'FontWeight', 'normal',...
'FontSize', 10);
x = uicontrol('Parent', TabHandles{1,1}, ...
'Units', 'pixels', ...
'Position', [ round(PanelWidth/5) 7.5*ButtonHeight ...
round(PanelWidth/10) round(PanelHeight/18) ],...
'Parent', TabHandles{1,1}, ...
'String', 'X [Ohm]', ...
'Style', 'edit',...
'HorizontalAlignment', 'center',...
'FontName', 'arial',...
'FontWeight', 'normal',...
'FontSize', 10);
uicontrol('Parent', TabHandles{1,1}, ...
'Units', 'pixels', ...
'Position', [ round(PanelWidth/6.5) 6.5*ButtonHeight ...
round(PanelWidth/13) round(PanelHeight/18) ],...
'String', 'Ver', ...
'Callback', @(r,x) VerCallback( haxes2, r, x), ...
'Style', 'pushbutton',...
'HorizontalAlignment', 'center',...
'FontName', 'arial',...
'FontWeight', 'bold',...
'FontSize', 10);
Adam
Adam am 8 Feb. 2017
Those two variable arguments to your verCallback (which you haven't shown so I have no idea what it does, but it doesn't have enough information to do what you want) are the source and the event data (the documentation details these). Anything beyond those you have to pass in yourself as you do the axes handle, unless you use a nested function.
You have named your edit boxes r and x so how can you expect a plot to plot a line involving r and x? I assume you mean str2double( r.String ), but your callback knows nothing about your uicontrols. The fact you called the variable arguments to the callback r and x doesn't matter, they are not the same r and x you have there they are just place holders for the source of the callback (which will be the pushbutton) and the event data (which is not very useful at all).

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 6 Feb. 2017

Kommentiert:

am 8 Feb. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by