Assignin && Evalin into Matlab variable struct

14 次查看(过去 30 天)
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 个评论
Image Analyst
Image Analyst 2016-9-25
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 2016-9-25
Hi, the struct is in the global workspace, the follow image cann be helpful

请先登录,再进行评论。

回答(2 个)

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

Image Analyst
Image Analyst 2016-9-25
编辑:Image Analyst 2016-9-25
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 个评论
Alex
Alex 2016-9-25
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 2016-9-26
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?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by