lundi 2 novembre 2015

How can I create a variable with a dynamic name based on a string in MATLAB?

I have the following MATLAB function that reads a variable var. For example, the struct cfg is passed as var. In this case, I succesfully recover this string representation as 'cfg' using inputname(1). I can also succesfully save as 'cfg.mat'. However, loading into memory as 'cfg' does not work. I have considered using eval, but I have not been able to make it work.

function data_from_mat = optimize2disk( var )
%OPTIMIZE2DISK Saves a variable to disk, deletes it from current memory,
% and loads it from disk
%   
    tempvar = inputname(1);
    disp(['Saving ', tempvar, ' to disk...']);
    save([pwd, '\\', tempvar, '.mat']);    
    disp(tempvar);
    data_from_mat = load([pwd, '\\', tempvar, '.mat']);
end





Aucun commentaire:

Enregistrer un commentaire