Toggle grid on off when user selects in a GUI
Mostrar comentarios más antiguos
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!
Respuestas (1)
Adam Danz
el 17 de Nov. de 2022
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 comentarios
Ben
el 17 de Nov. de 2022
Adam Danz
el 17 de Nov. de 2022
The demo is functional but that doesn't mean anyone can copy-paste it into their code without slightly adapting it to match function names and variable names.
The change you made to the 3rd input argument, changing ax to g.ax1, is not correct and likely caused an error. You are passing in the value of g.ax1 but within the callback function, the variable name I chose was ax. You can choose a different variable name but g.ax1 is not a variable name.
I assumed g.ax1 is an axes handle -- is this true? If not, you should pass in an axis handle rather than g.ax1.
Here what you should troubleshoot
- Fix the mistake mentioned above. Use the version in my answer.
- Make sure you're passing in an axis handle
- Did you also make the first change in my answer? ("callback", {@Callback_grid1, g.ax1},...)
- Are there any error messages?
Ben
el 17 de Nov. de 2022
Adam Danz
el 17 de Nov. de 2022
Good. That's what I assumed. h.ax# are axes.
That takes care of item #2 in my troubleshooting list above.
Categorías
Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!