how to switch YLim from auto to manual using contextmenu

4 ビュー (過去 30 日間)
G A
G A 2013 年 1 月 19 日
The following code works OK, switching y limit in Fig3_axes.
plot_into3=handles.Fig3_axes;
ylim_menu = uicontextmenu;
hcb1 = 'set(gco, ''YLimMode'',''auto'')';
hcb2 = 'set(gco, ''Ylim'',[0,1])';
uimenu(ylim_menu, 'Label', 'auto', 'Callback', hcb1);
uimenu(ylim_menu, 'Label', 'manual', 'Callback', hcb2);
set(plot_into3,'uicontextmenu',ylim_menu);
However, when I try to replace [0,1] by a variable
Fig3yLim=[0,1];
hcb2 = 'set(gco, ''Ylim'',Fig3yLim)';
I am getting an error:
Undefined function or variable 'Fig3yLim'.
Error while evaluating uimenu Callback
How can I use the variable?

採用された回答

Jan
Jan 2013 年 1 月 19 日
編集済み: Jan 2013 年 1 月 19 日
Yes, of course. Callbacks are evaluated in the base workspace. If you use a variable, which is defined in a function, it is not known when the callback runs.
Better use function for callbacks and store variables in the figure by guidata.
uimenu(ylim_menu, 'Label', 'auto', 'Callback', {@uimenu_Callback, 'auto'});
uimenu(ylim_menu, 'Label', 'manual', 'Callback', {@uimenu_Callback, '01'});
...
function uimenu_Callback(uimenuH, EventData, Command)
handles = guidata(uimenuH);
switch Command
case '01'
set(handles.Fig3_axes, 'YLim', [0, 1]);
case 'auto'
set(handles.Fig3_axes, 'YLimMode', 'auto');
otherwise
error('Unknown command - programming error');
end
Now the contents of the variable Fig3yLim can by added as 4th input argument.
Defining callbacks as strings is supported for backward compatibility with Matlab 5.3 (1999). Using function handles is more secure and efficient.
  1 件のコメント
G A
G A 2013 年 1 月 19 日
Thanks Jan, it works now.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by