Problem with global variables.
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Stephen23
am 3 Aug. 2017
Bearbeitet: Stephen23
am 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.
Akzeptierte Antwort
Jan
am 3 Aug. 2017
Bearbeitet: Jan
am 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 Kommentare
Stephen23
am 4 Aug. 2017
"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.
Jan
am 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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Use Prebuilt MATLAB Interface to C++ Library finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!