Toggle grid on off when user selects in a GUI

11 ビュー (過去 30 日間)
Ben
Ben 2022 年 11 月 17 日
コメント済み: Adam Danz 2022 年 11 月 17 日
Hola everyone - this is my first FE post.
I have a GUI, it loads in 4 seperate graphs on theb GUI figure when I push a button, the graphs are called:
g.ax1 - graph 1, g.ax2 - graph 2 and so forth.
In a seperate 'checkbox', which I position next to the figure, I want to turn the button on and off to plot the grid lines on the plots.
How do I specify which figure the check box will work on? E.g. checkbox clicked for g.ax1, turns the grid on for for g.ax1. Checkbox clicked for g.ax2, turns the grid on for figure g.ax2. The plotted data comes from a seperate function with its own button callback.
I tried the below, nothing works
This is the check box for graph 1 g.ax1
h.grid_checkbox1 = uicontrol ("style", "checkbox",
"units", "normalized",
"string", "Check to turn grid on",
"value", 0,
"callback", @Callback_grid1,
"position", [0.0 0.1 0.1 0.3]);
This is the function to hopefully turn the grid on and off for a specific figure, done seperately for g.axa and g.ax2 as examples:
%Checkbox to turn the grid on and off for g.ax1
function @Callback_grid1(hObj, event)
v1 = get (gca,g.ax1);
grid (merge (v, "on", "off"));
end
The below is for turning the grid on and off for g.ax2
function @Callback_grid2(hObj, event)
v2 = get (gca,g.ax2);
grid (merge (v, "on", "off"));
end
It seems required that the figure is refered to somehow to turn the grid on.
Muchas Gracias!

回答 (1 件)

Adam Danz
Adam Danz 2022 年 11 月 17 日
Great question. The funcion gca gets the current axes which is the axes most recently in focus. Instead, you want to specify which axes to apply the grid.
1. First, supply the axes handle in the callback function definition
h.grid_checkbox1 = uicontrol ("style", "checkbox",...
"units", "normalized",...
"string", "Check to turn grid on",...
"value", 0,...
"callback", {@Callback_grid1, g.ax1}, ... % <-----------
"position", [0.0 0.1 0.1 0.3]);
2. Add the axis handle as an input to the callback function, and simplify the on/off toggle by using checkbox object properties
function Callback_grid1(hObj, event, ax) % <-----------
% hObj is the handle to the checkbox
% ax is the axis handle
% Turn grid on/off depending on checkbox value
grid(ax,hObj.Value); % The value (1/0) will toggle the grid
end
  4 件のコメント
Ben
Ben 2022 年 11 月 17 日
My figure is created like this
g.ax1 = axes ("position", [0.01 0.01 0.2 0.1]);
g.ax2 = axes ("position", [0.2 0.1 0.2 0.1]);
Perhaps I need to name the figure?
Adam Danz
Adam Danz 2022 年 11 月 17 日
Good. That's what I assumed. h.ax# are axes.
That takes care of item #2 in my troubleshooting list above.

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

カテゴリ

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