Problem with global variables.

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.
e.g.
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);
end
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!

4 Comments

Benjamin Attal
Benjamin Attal on 3 Aug 2017
Edited: Benjamin Attal on 3 Aug 2017
Can you attach your main file as well, or the part of your main file that references `sld`?
First of all thanks for your fast reply. I don't even have to try to access the sld struct in the main in order to make it crash. E.g. a project I made some weeks ago (added the relevant part of the code, the whole thing would be some thousand lines). Simply executing it in Matlab R2017a will make it crash. On R2016a it works completely fine: I can reset the value, change them and then they get saved in p.
Stephen23
Stephen23 on 3 Aug 2017
Edited: Stephen23 on 3 Aug 2017
"how I could rewrite my code so it works the way I want it to?"
Simple: do not use global variables. Search this forum to know why. Start by reading this:
And then use one of the more reliable methods described in the MATLAB documentation:
Personally I find nested functions very intuitive and reliable for the kinds of situations that you describe.
Thanks a lot. I will have a look at it. Should solve my problem!

Sign in to comment.

 Accepted Answer

Jan
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.

4 Comments

You got a point there. I'll rewrite the thing that I am currently working on that way. I didn't even notice... Wrote the whole file in 1.5h hours... Probably should have taken more time and programmed it cleaner... But it is still weird that it works in R2016a but not in R2017a. Thanks a lot!
It was the first time writing an UI in Matlab for me so I was very unfamiliar and i chose the first thing that came to my mind, global variables... After reading a bit I see why it is lethal to use it in Matlab... Thanks again for the tips and I'll try to do better...
"After reading a bit I see why it is lethal to use it in Matlab"
This is not about MATLAB! Global variables are a bad idea in almost every modern programming language. The internet will tell you why.
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)

Asked:

on 3 Aug 2017

Commented:

Jan
on 4 Aug 2017

Community Treasure Hunt

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

Start Hunting!