MATLAB Answers

How to load a variable into the workspace not as a structure into a GUI?

39 views (last 30 days)
Adriano
Adriano on 27 Nov 2017
Commented: Stephen Cobeldick on 27 Nov 2017
Hi everybody, I'm developing a GUI and I need to load a variable into the workspace. I'm using the "assignin" function but it generates a struct variable. This is my code:
function popupmenu1_callback(hObject, eventdata, handles)
asset = 'XYZ';
assignin('base',asset, load('XYZ.mat'));
end
the output is XYZ struct variable. Thus, to get my data I must do XYZ.XYZ . How can avoid that and to get directly the XYZ variable (not like a struct)? Many thanks!

  1 Comment

Stephen Cobeldick
Stephen Cobeldick on 27 Nov 2017
As Jan Simon mentioned, making variables magically appear in other workspaces is not recommended. Doing so will make your code slow, complex, buggy, and hard to debug. Read this to know why:

Sign in to comment.

Accepted Answer

Jan
Jan on 27 Nov 2017
Remember, that the remote controlled creation of variables in another workspace is a bad programming style. I would avoid this strictly, but store the data inside the figure and use it from there.
But if you really want to do this:
asset = 'XYZ';
Data = load('XYZ.mat');
assignin('base', asset, Data.(asset));

More Answers (0)