Range variables callback issue
3 views (last 30 days)
Show older comments
Julien Maxime on 26 Jan 2023
Commented: Julien Maxime on 30 Jan 2023
I have created manually a GUI and I am looking for help reguarding a small issue that i have. In short, i have two graphs in my uifigure and I would like their y range to be modified when I change numbers in uieditfield textboxs, one for MAX and one for MIN, that i added on the GUI.
When I do it for one it works fine, example i put 500 in MAX textbox and it indeed changes the max range for both plots, but right after, when I change the MIN textbox, the MAX value is reset to default. My code is quite extensive so I put below only the parts referring to the textboxes and feedback loops.
I first tried with only two variables, maxC and minC and then added two global variables (i know its bad) in the hope that I would save the input values but does not changes anything.
Sorry if it is messy,
And thank you for your help,
% Initialisation of min and max values with the highest and lowest values
% in my data matrix
maxC = max(F, , 'all');
minC = min(F, , 'all');
%Global variables definition
%Definition of textboxes and feedbackloops accordingly. ax and ax1 are my
tlabel3 = uilabel(fig);
tlabel3.Text = 'Range - Max';
tlabel3.Position = [965 730 150 200];
tlabel3.HorizontalAlignment = 'left';
tlabel3.VerticalAlignment = 'bottom';
'Position', [970 700 60 20], ...
"ValueChangedFcn",@(edtMax,event) textChangedMax(edtMax, gminC, ax, ax1));
tlabel4 = uilabel(fig);
tlabel4.Text = 'Range - Min';
tlabel4.Position = [1067 730 150 200];
tlabel4.HorizontalAlignment = 'left';
tlabel4.VerticalAlignment = 'bottom';
'Position', [1070 700 60 20], ...
"ValueChangedFcn",@(edtMin,event) textChangedMin(edtMin, gmaxC, ax, ax1));
function textChangedMax(edtMax, gminC, ax, ax1)
maxC = str2double(edtMax.Value);
gmaxC = maxC;
ylim(ax1, [gminC gmaxC]);
function textChangedMin(edtMin, gmaxC, ax, ax1)
minC = str2double(edtMin.Value);
gminC = minC;
ylim(ax1, [gminC gmaxC]);
Stephen23 on 26 Jan 2023
"I have created manually a GUI ..."
then using the nested functions is by far the best way to pass data between workspaces in your GUI. Forget about evil GLOBAL variables and lots of anonymous functions, just write your callbacks as nested functions.
Rik on 27 Jan 2023
I fully agree with Stephen that you should not use global variables. There are more legitimate uses than eval, but not many.
The source of your problem is the you do not declare your global variables in your callback functions, meaning they are simply local variables.
For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
I personally prefer sharing data with guidata, but nested functions will work as well.
More Answers (0)
Find more on Plot Customization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!