Assignin && Evalin into Matlab variable struct

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

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?
sorry, i wanted to say, i want to modify a struct wich is in my workspace under the name "Fahrzeug" from the GUI
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?
Hi, the struct is in the global workspace, the follow image cann be helpful

Accedi per commentare.

Risposte (2)

https://se.mathworks.com/matlabcentral/answers/498856-using-assignin-with-structure-elements#answer_408618
Image Analyst
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

Hi,
it not working Unfortunately .
i think the problem is rather by using assisgn with the Struct.
Image Analyst
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?
Hi, i have always work with assignin and evalin, and im under time pressure thats why, i would like to avoid to try something else. i have uploaded 3 files but it is little bit difficult to follow the gui code all u need is the popup menu window and the callbacks of them. u can delete the rest of the code otherwise u will get a lot of errors
many thnx
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.
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
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?

Accedi per commentare.

Categorie

Tag

Richiesto:

il 25 Set 2016

Risposto:

il 30 Nov 2022

Community Treasure Hunt

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

Start Hunting!

Translated by