Problem with global variables.
Show older comments
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
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`?
tf_eng
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.
tf_eng
on 3 Aug 2017
Accepted Answer
More Answers (0)
Categories
Find more on Use Prebuilt MATLAB Interface to C++ Library in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!