Clear Filters
Clear Filters

Assignin && Evalin into Matlab variable struct

14 views (last 30 days)
Alex
Alex on 25 Sep 2016
Answered: Sanatjon Gofurov on 30 Nov 2022
Hi everybody,
I'm trying to modify fields a structure from inside a gui "popupmenu". I'm trying to use the assignin function.
contents = get(hObject,'Value')
Klima_Regler_GUI = evalin('base','Fahrzeug.Klima_Regler');
switch contents
case 1
Klima_Regler_GUI = 1;
assignin('base','Fahrzeug.Klima_Regler',Klima_Regler_GUI);
case 2
Klima_Regler_GUI = 0.75;
assignin('base','Fahrzeug.Klima_Regler',Klima_Regler_GUI);
case 3
Klima_Regler_GUI = 0.50;
assignin('base','Fahrzeug.Klima_Regler',Klima_Regler_GUI);
case 4
Klima_Regler_GUI= 0.25;
assignin('base','Fahrzeug.Klima_Regler',Klima_Regler_GUI);
case 5
Klima_Regler_GUI = 0;
assignin('base','Fahrzeug.Klima_Regler',Klima_Regler_GUI);
otherwise
end
but i keep getting this erorr ( Invalid variable name "Fahrzeug.Klima_Regler" in ASSIGNIN )
i wil appreciate any help
  4 Comments
Image Analyst
Image Analyst on 25 Sep 2016
Which workspace? Every function has it's own temporary workspace that is in existence for as long as the function is running? What other workspace do you have? There is a "global" workspace that comes into view if you ever use the "global" statement - do you have any global variables in the global workspace?
Alex
Alex on 25 Sep 2016
Hi, the struct is in the global workspace, the follow image cann be helpful

Sign in to comment.

Answers (2)

Sanatjon Gofurov
Sanatjon Gofurov on 30 Nov 2022
https://se.mathworks.com/matlabcentral/answers/498856-using-assignin-with-structure-elements#answer_408618

Image Analyst
Image Analyst on 25 Sep 2016
Edited: Image Analyst on 25 Sep 2016
You've just said the variable (a structure) is in the global workspace, not the base workspace. Yet you're using evalin('base') to try to retrieve it from the base workspace. That won't work because it's not in the base workspace.
Think of workspaces like buckets. There is (or may be) a base workspace bucket, maybe a global bucket (if you declared anything global), and temporary buckets for each function that exist while they're being executed. The buckets hold all the variables for that workspace while it is in scope. The base and global buckets may or may not be empty, depending on if you explicitly put something into them. Your problem is that you're looking in the wrong bucket!
To access variables in the global workspace, you need to use the global statement in every function where you want to access global workspace variables. So as the first line of your popup callback, have this:
global Fahrzeug;
  6 Comments
Alex
Alex on 25 Sep 2016
Hi,
i tried ur code, now i dont get the error with the assisgn but i still have the problem that the value of Fahrzeug.Klima_Regler get not overwritet with the value off Klima_Regler_GUI.
and thats why i used at the begining assisgn
Image Analyst
Image Analyst on 26 Sep 2016
Well then if you want to send the value from your program back to the base workspace, you'll have to use assignin(). Personally, I think that's a bad way to have your program work. Why do you need to send the value back to the base workspace?

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!