0

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
noumenal
  • 1,077
  • 2
  • 16
  • 36
  • 3
    Dont :(. Dynamic variables are not cool. – Ander Biguri Nov 02 '15 at 15:08
  • @AnderBiguri How is this helpful feedback? My goal is to reduce the probability of `OUT OF MEMORY` errors on a 32-bit system. Is there another way to solve my problem? `Pack` is not sufficient – noumenal Nov 02 '15 at 15:12
  • So why do you save and then load back? Does that solve any of your problems? after the run of the code, the amount of memory is the same , rigth? – Ander Biguri Nov 02 '15 at 15:29
  • 1
    @noumenal this is as helpful as feedback can get. Not the answer to your specific question, but *very* helpful feedback. – Andras Deak -- Слава Україні Nov 02 '15 at 15:43
  • @AndrasDeak What I mean by example: "GOTO statements are not cool. Don't do it." (not helpful, creates a different problem: refactoring) "Instead you have to..." (helpful: solves a problem) or "...should be avoided because..." (maybe not helpful but meaningful). Anyhow, the resulting answer was satisfying to my needs. – noumenal Nov 02 '15 at 16:31
  • 1
    Check the links in [this answer](http://stackoverflow.com/questions/32467029/how-to-put-these-images-together/32467170#32467170), they explain quite well why using dynamic variable names and especially `eval` is bad. Gist: it breaks the interpreter and therefore all built-in acceleration engines – Adriaan Nov 02 '15 at 16:37
  • @Adriaan. Thanks, one of the sources says "don't create variables at runtime using EVAL unless you have a very good reason". In this case I was looking for a way to capture the passed reference and not the value. Java solves this thorugh reflection, so obviously if this functionality is implemented there are cases where it would make sense. – noumenal Nov 02 '15 at 16:49

1 Answers1

2

To load it from memory do:

load([pwd, '\\', tempvar, '.mat']);

and it will load whatever it was inside with the names they were save with, or else do

data_from_mat=load([pwd, '\\', tempvar, '.mat']);

and data_from_mat will be a structure with fields corresponding to the name of the variables that were saved, thus

data_from_mat=getfield(data_from_mat,'varname'); %// instead of 'varname' tempvar?

will give you the data itself.

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120