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
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?
回答 (2 件)
Sanatjon Gofurov
2022 年 11 月 30 日
https://se.mathworks.com/matlabcentral/answers/498856-using-assignin-with-structure-elements#answer_408618
0 件のコメント
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 件のコメント
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 Center および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!