Assignin && Evalin into Matlab variable struct
Mostra commenti meno recenti
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 Commenti
Image Analyst
il 25 Set 2016
Why are you so sure that anything is in the base workspace? Did you run a script in addition to running your GUIDE-built program?
Alex
il 25 Set 2016
Image Analyst
il 25 Set 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
il 25 Set 2016
Risposte (2)
Sanatjon Gofurov
il 30 Nov 2022
1 voto
https://se.mathworks.com/matlabcentral/answers/498856-using-assignin-with-structure-elements#answer_408618
Image Analyst
il 25 Set 2016
Modificato: Image Analyst
il 25 Set 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 Commenti
Alex
il 25 Set 2016
Image Analyst
il 25 Set 2016
Modificato: Image Analyst
il 25 Set 2016
Honestly, I never, ever use evalin() or assignin(). I never have needed to and I don't think you should need to either. Why don't you see the FAQ for ways to share variables between functions and programs: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
What if you just use evalin() on Fahrzeug, without the Klima_Regler field.
Other than that the only suggestion I would have is to post your .m and .fig files.
By the way, it looks from the screenshot like your variable is in the base workspace, not the global workspace. Anyway, I think it's bad programming practice to have your GUI, which I think should be a pretty self contained entity, to depend on some variables being in the base workspace. If you need certain variables, create them yourself. I mean what if the user never ran some other script to place things in the base workspace before you needed them?
Alex
il 25 Set 2016
Image Analyst
il 25 Set 2016
I've looked over the code and I'd like you to try something easy first. Just put the line
global Fahrzeug;
into the functions Klima_Steuerung_Callback(), Geschwindigkeit_Begrenzung_Callback(), and popupmenu4_Callback(). Those are the only other functions that use the Fahrzeug variable.
Then, get rid of any evalin() calls and replace assignin()'s like this
assignin('base','Fahrzeug.Klima_Regler',Klima_Regler_GUI);
with simple assignments like this:
Fahrzeug.Klima_Regler = Klima_Regler_GUI;
And if Fahrzeug really was created by some other program, you'll need to import it. It's probably simplest to put it into your opening function.
function Schnittstelle_OpeningFcn(hObject, eventdata, handles, varargin)
global Fahrzeug;
% Get from Fahrzeug from the base workspace.
evalin('base','Fahrzeug');
If it's not there, it will throw an error like:
Error using evalin
Undefined function or variable 'Fahrzeug'.
Let me know how it works out.
Alex
il 25 Set 2016
Image Analyst
il 26 Set 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?
Categorie
Scopri di più su Variables in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
