With the following code I’ve added variables into the base workspace:
function data_startup()
bdclose all;
data=load(fullfile('B', 'C', 'data.mat'));
file_variables=fieldnames(data);% get the field names as cell array
for ii=1:length(file_variables)
assignin('base', file_variables{ii}, data.(file_variables{ii}));
end
Now, I would like to clear variables from the base workspace, I’ve tried :
evalin('base','clear file_variables');
But this is not working 🙁
You’re trying to clear the variable with name
file_variables, which probably doesn’t exist. What you want is:If you want to understand what’s going on: run and debug this line in your function, then inspect the outcome of
which will be the command run by
evalin.More info:
[a b c]concatenates the strings ina,bandc, because the strings themselves are1xNarrays.{:}returns the cell array as a comma separated list which is then inputted tosprintf, more info on this here.