Range variables callback issue

8 ビュー (過去 30 日間)
Julien Maxime
Julien Maxime 2023 年 1 月 26 日
コメント済み: Julien Maxime 2023 年 1 月 30 日
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
global gminC;
global gmaxC;
%Definition of textboxes and feedbackloops accordingly. ax and ax1 are my
%two graphs
tlabel3 = uilabel(fig);
tlabel3.Text = 'Range - Max';
tlabel3.Position = [965 730 150 200];
tlabel3.HorizontalAlignment = 'left';
tlabel3.VerticalAlignment = 'bottom';
uieditfield(fig, ...
'Position', [970 700 60 20], ...
'Value',sprintf('%d',maxC), ...
"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';
uieditfield(fig, ...
'Position', [1070 700 60 20], ...
'Value',sprintf('%d',minC), ...
"ValueChangedFcn",@(edtMin,event) textChangedMin(edtMin, gmaxC, ax, ax1));
%Feedback functions
function textChangedMax(edtMax, gminC, ax, ax1)
maxC = str2double(edtMax.Value);
gmaxC = maxC;
clim(ax,[gminC gmaxC]);
ylim(ax1, [gminC gmaxC]);
function textChangedMin(edtMin, gmaxC, ax, ax1)
minC = str2double(edtMin.Value);
gminC = minC;
clim(ax,[gminC gmaxC]);
ylim(ax1, [gminC gmaxC]);
  1 件のコメント
Stephen23 2023 年 1 月 26 日
"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 2023 年 1 月 27 日
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.
  1 件のコメント
Julien Maxime
Julien Maxime 2023 年 1 月 30 日
I used guidata and it worked very well. Actually thinking about it, as my funtion is basically only focused on the GUI, the guidata is analogous to global variables but without all the troubles, which is very convenient.
Thank you for your answers !


その他の回答 (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