Range variables callback issue

조회 수: 2 (최근 30일)
Julien Maxime
Julien Maxime 2023년 1월 26일
댓글: Julien Maxime 2023년 1월 30일
Hello,
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;
gminC=minC;
global gmaxC;
gmaxC=maxC;
%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]);
end
function textChangedMin(edtMin, gmaxC, ax, ax1)
minC = str2double(edtMin.Value);
gminC = minC;
clim(ax,[gminC gmaxC]);
ylim(ax1, [gminC gmaxC]);
end
  댓글 수: 1
Stephen23
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
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 CenterFile Exchange에서 Install Products에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by