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:
evalin('base',['clear' sprintf(' %s',file_variables{:})]);
If you want to understand what's going on: run and debug this line in your function, then inspect the outcome of
['clear' sprintf(' %s',file_variables{:})]
which will be the command run by evalin.
[a b c] concatenates the strings in a,b and c, because the strings themselves are 1xN arrays.
{:} returns the cell array as a comma separated list which is then inputted to sprintf, more info on this here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With