Convert lokal variables in to Global varibles.
Show older comments
i am using gui now in my Project. In order to see some results and use in further pushbuttons i used assignin .However when i want to use These structure Array which i created and made visisble by using assignin,i can not use it unfortunately. do you know any idea haw can i convert local variables in to global variables
Answers (2)
Jan
on 9 Mar 2015
2 votes
By the way: Using assignin and global variables are bad programming practice. Try to avoid it and store the variables in the GUIs ApplicationData, e.g. by the guidata command. Search in this forum for "assignin" to get more opinions to this topic.
5 Comments
Although this sounds like an easy option, this can be complicated for beginners as GUIDE uses guidata for its own purposes and does not let you store data without first adding fields to the handles structure. The docs say:
GUIDE Uses guidata GUIDE uses guidata to store and maintain the handles structure. In a GUIDE code file, do not overwrite the handles structure or your program will no longer work. If you need to store other data, you can do so by adding new fields to the handles structure.
Adam
on 9 Mar 2015
Why do you want to see variables in your base workspace when you are running a GUI front-end?
If you intend to use the results in other pushbutton callbacks you do not need the variables to go anywhere near the base workspace.
Using guidata is a two-way process - you push data into the GUI's stored data in one callback and in another you can pull it out again to use within the scope of that callback.
This need to use assignin is apparently common with German MATLAB users: I'm fascinated by the peculiar differences between question in goMatlab and MATLAB Answers: While the majority of German speaking beginners seem to transfer variables to functions by ASSIGNIN and EVALIN (and suffer from the resulting difficulties), this does rarely appear here
Quote from Jan Simon:
@cemsi888: Learn to avoid assignin , those variables already have their own workspace, they don't need to be in the base workspace too! Use the debugging tools if you want to have a look at them while you are writing your code.
@Jan Simon: any idea on the origin of this cultural difference?
cemsi888
on 10 Mar 2015
function setGlobalx(val)
global x
x = val;
and
function r = getGlobalx
global x
r = x;
which you can use to store a value, and retrieve it later using, even from a different workspace.
1 Comment
Image Analyst
on 9 Mar 2015
And read the FAQ too. http://matlab.wikia.com/wiki/FAQ#Are_global_variables_bad.3F If you're careful you can use globals without problem. Of course if you do dumb things in a function like assign a variable that you shouldn't have, then outside of that function the bad value will be seen regardless if you assigned it to a global variable or used setappdata()/getappdata() to assign/retrieve it.
Categories
Find more on Scope Variables and Generate Names in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!