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

回答 (1 件)

Adam
Adam 2017 年 2 月 6 日
編集済み: Adam 2017 年 2 月 6 日

0 投票

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 件のコメント

Matheus Silva
Matheus Silva 2017 年 2 月 7 日
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 2017 年 2 月 8 日
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).

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeInteractive Control and Callbacks についてさらに検索

質問済み:

2017 年 2 月 6 日

コメント済み:

2017 年 2 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by