Hello everybody,
I've got a problem in which I want to generate variables of a structure I've got a structure called newData which consists of data, given by the matrix 'data' and the imported text in the cell called 'textdata'. I want to seperate the two variable in the structure. Before I used the following code
newData = importdata(file, ' ', 6); %imports data
% Create new variables in the base workspace from those fields.
vars = fieldnames(newData); % which are 'data' and 'textdata'
for i = 1:length(vars)
%this should create the variables 'data' and 'texdata'
assignin('base', vars{i}, newData.(vars{i}));
end
%which results in an error, as 'textdata' is not created by the assignin.
headers = char(textdata(6,:));
The error is generated by the fact that apparently, the variables data and textdata are not created (or not created in the right workspace) and consequently matlab cannot find these variables in further calculations (in this case the creation of headers cannot be executed, since 'textdata' is unknown.)
I used this piece before in a single m-file, and this worked perfect, But now I'm incorporating this in a GUI, so i need to use this within a function, which results in errors. I've also tried to write this piece in a different function m-file, which yields the same results. Basically, the problem is: how to use assignin within a (GUI) function, or perform the same action without using assignin?
I hope the problem is clear, and you guys can help me out.
Sincerely,
Roy

5 commentaires

Walter Roberson
Walter Roberson le 15 Juin 2012
What error do you encounter? assigin 'base' is valid within callbacks.
Roy Veldhuizen
Roy Veldhuizen le 15 Juin 2012
I do not really get an error. I think it is generating the variables, but then is setting it to a different workspace. As such, the error that I receive is when i call the variables as defined by vars, they cannot be found by Matlab.
Walter Roberson
Walter Roberson le 15 Juin 2012
How do you call upon the variables? Remember that functions do not run in the base workspace, so unless you evalin('base') of the variable, you are not going to be able to see the value you created.
Roy Veldhuizen
Roy Veldhuizen le 18 Juin 2012
Well my code looks like this
newData = importdata(file, ' ', 6); %imports data
% Create new variables in the base workspace from those fields.
vars = fieldnames(newData); % which are 'data' and 'textdata'
for i = 1:length(vars)
assignin('base', vars{i}, newData.(vars{i})); %which should create the variables 'data' and 'texdata'
end
headers = char(textdata(6,:)); %which results in an error, as the textdata variable is not created by assignin.
I've tried using the options that are listed under http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.3F but these only address the problem of passing the variable from one callback to another.
When I use the debugger, I do not see the variables being created and thus I think the issue generating the variables, rather than passing them from one callback to another..
Walter Roberson
Walter Roberson le 18 Juin 2012
assignin('base') writes data into the base workspace, which is the workspace that is active when you are not inside any function (e.g., the workspace you get if you start assigning variables at the command line just after entering MATLAB.)
assignin('base') does NOT write variables into the current workspace (not unless the current workspace _is_ the base workspace, such as if you are executing a script from the command line.)
You write variables into the base workspace but then you try to use them in the current workspace.
headers = char(evalin('base', 'textdata(6,:)'));

Connectez-vous pour commenter.

 Réponse acceptée

Walter Roberson
Walter Roberson le 15 Juin 2012

0 votes

Please consider the alternatives to using the base workspace: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.3F

1 commentaire

Neel Maharaj
Neel Maharaj le 30 Avr 2021
Love this! Stackoverflow Simulator: Matlab edition

Connectez-vous pour commenter.

Plus de réponses (1)

Roy Veldhuizen
Roy Veldhuizen le 27 Juin 2012

0 votes

Based on documentation I decided that it was best not to use assignin, and used on of the methods specifically exporting to a mat file as given by Walter: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.3F

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by