How can I pass variable from GUI to matlab workspace?

The global function is nice, for example:
(in GUI):
global y
y=5;
and then in matlab command window i type global y and it is done.
The way I want to do it is that y is automatticaly printed in workspace without me typing global y in command window. Can it be done?
Thanks.

5 comentarios

Stephen23
Stephen23 el 3 de Abr. de 2017
Editada: Stephen23 el 24 de Dic. de 2021
Rather than using awful and buggy global variables, or buggy and slow assignin, you should use the approaches given in the MATLAB documentation, which describes efficient ways of passing data between callback functions:
Yoni Sher
Yoni Sher el 26 de Dic. de 2017
Hi, I for one need the variable in global space so I can access it from the command window, so your answer (as well as being slightly rude) is not helpful - what you answered simply isn't the question that was asked.
Jan
Jan el 26 de Dic. de 2017
Editada: Jan el 26 de Dic. de 2017
@Yoni: Global variables are known to cause more troubles then they solve. Therefore experienced programmers consider them as a bad programming practice. Of course your can write a program such that accessing global variables in the base workspace is needed. But then the best solution in a long term view is to change the programming style. Therefore Stephen's answer is really helpful, maybe more than you are aware of currently.
Stephen's words "awful", "buggy" and "slow" concern the global variables and not the asking person. Therefore he is not rude at all. Maybe it helps you to understand Stephen's intention if you take into account, that this topic has discussed hundreds of times already.
In this link, could you please point a little bit more, where should I look at, because this is a long article and I still cannot find a way to pass GUI input data to workspace.
@Nafiz: I do never create variables remotely in another workspace. I consider this as too confusing and prone to bugs. But if you have a really good reason to do this, it is easy:
assignin('base', 'V', data)
Now the variable V is created in the base workspace (accessible in the command window) and set to the value data.
The linked article concerns another problem: Sharing data between callbacks. Then using the base workspace is a bad idea, because it is much better to store the data inside the figure, in the UserData or ApplicationData, e.g. using setappdata or guidata.

Iniciar sesión para comentar.

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 4 de Nov. de 2012
Editada: Azzi Abdelmalek el 4 de Nov. de 2012
don't use semicolon ;
y=5

8 comentarios

Tomislav
Tomislav el 4 de Nov. de 2012
do you mean like:
global y=5; ?
if do like this error comes up: The expression to the left of the equals sign is not a valid target for an assignment.
no
global y
y=5
you don't need to use global, just type your line command without semicolon
y=5
not
y=5;
Tomislav
Tomislav el 4 de Nov. de 2012
Editada: Tomislav el 4 de Nov. de 2012
Not quite what I wanted. This way it is processed in command window but still not visible in workspace as a variable to work with
what do you need exactly? If you want to use it in other function of your gui, there is better way
Tomislav
Tomislav el 4 de Nov. de 2012
I need to have it in workspace because in my GUI there is editbox where I put this variable name and work with it from that point forward...
If it is not in workspace the other function in GUI wont recognize it.
If there is some other way to achive this please post it.
put your data in userdata of any object for eg:
set(handles.editbox,'userdata',y)
to get your variable
y=get(handles.editbox,'userdata')
Tomislav
Tomislav el 4 de Nov. de 2012
That is if I want automatically put variable in editbox witch I can do manually it is important that this variable is shown in main matlab workspace.
Masoud
Masoud el 18 de Jul. de 2015
Editada: Masoud el 18 de Jul. de 2015
Thank you
That was really helpful
good luck
Mehrabi

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

el 4 de Nov. de 2012

Editada:

el 24 de Dic. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by