Problem with global variables.

38 views (last 30 days)
tf_eng on 3 Aug 2017
Commented: Jan on 4 Aug 2017
Hey everybody! I am having issues with Matlab dialog boxes lately. A particular syntax that I used to use in R2016a is not working in R2017a anymore. I have many sliders in my dialog box and when I am closing the dialog box I want them to be saved to variables so I combined them to a struct, so the Callback function of the 'Apply' button could access them. I also need to adjust slider limits depending to what other sliders are set to. So the struct structure with global structs that the Callbacks could modify came in very handy and worked completely fine.
function Test
global sld;
fig=uifigure('Position',[400 400 200 200]);
sld.A=uislider(fig,'Position',[100 100 20 3],'Limits',[0 1],'Value',1);
I don't get any errors if I execute this function directly in Matlab R2017a but if I call the function from my main file it crashes and brings up the error:
No public property A exists for class matlab.ui.control.Slider.
Error in Test (line 4)
sld.A=uislider(fig,'Position',[100 100 20 3],'Limits',[0 1],'Value',1);
If I execute the code now directly from the file it won't work as well! Has anybody an idea what my mistake is or how I could rewrite my code so it works the way I want it to? Thanks already in advance!
tf_eng on 3 Aug 2017
Thanks a lot. I will have a look at it. Should solve my problem!

Sign in to comment.

Accepted Answer

Jan on 3 Aug 2017
Edited: Jan on 3 Aug 2017
Global variables are a shot in your knee. Again your problem shows, how easy it is to produce confusion using gloabls and how hard it is to debug the problem. The general rule is: Do not use globals.
The current mixing the of the data, you want to export finally, and the handles of the GUI, is not useful.
In the 'ValueChangedFcn' of the "sld.L" slider you provide the variables "@(sld,event)" as inputs to UpdateValues(). There you do not use this input, but the global variable "sld". It seems like this call overwirte "sld" with the handle of the slider. And then the confusion is perfect.
sld.L = uislider(fig,'Position',[50 725 200 3],'Limits',[0.05 2], ...
'Value',p.L,'FontName',Font, ...
'ValueChangedFcn',@(sld,event) UpdateValues);
It would be much better, saver and more clear, if you store the data struct in the ApplicationData or UserData (see: setappdata, guidata, set(FigH, 'UserData')). Then I assume that the problem will vanish magically, and even if it is still present, you can control easily where and when the value changes.
Jan on 4 Aug 2017
The problem does not concern only modern programming languages, but your personal properties also. Note that in theory nothing is lost, if everyone stores the money directly on the street in a single box, and not in individual pockets. Then you could get the money you need out of this box, or even better: tell the vendor, that he is allowed to take out the money he has earned by selling you something. It will take a few hours only until the the situation gets chaotic, although the system is perfect and deterministic.
Another example: What would happen if nearly all smart phone users would store and share their images in one central service? The users would loose control over what their photos are used for. You could create a world wide catalog of faces, recognize the relationships between persons, and influence their lives easily. Hum, well, let me think over this again. Let's concentrate on core message: Store data as locally as possible.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!