views:

53

answers:

1

I want to save the result of the q_yearly for each case_no in corresponding string of the q_cases_yearly as a .mat file. With my statement of save(q_cases_yearly{case_no},'q_yearly') even though the names of files are coming as the corresponding string of q_cases_yearly, however all those .mat files contain variable with the same name of q_yearly. When I open those .mat files I get a variable with name q_yearly for all the 3 files. However, I want the names of the variables stored in those files same as the name of the files i.e. q_5wells_yearly_CMG, q_explorwell_yearly_CMG and q_seismic_yearly_CMG respectively.

Here's my piece of code:

nModel=100;
nModel_want=1;
nCell=2500;
T=20;

ReservoirModel_CMGBuilder={'ReservoirModel_5wells_CMGBuilder','ReservoirModel_ExplorWell_CMGBuilder','ReservoirModel_Seismic_CMGBuilder'};
poro_models_gslib_file={'poro_models_5wells_gslib_format','poro_models_explorwell_gslib_format','poro_models_seismic_gslib_format'};
perm_models_gslib_file={'perm_models_5wells_gslib_format','perm_models_explorwell_gslib_format','perm_models_seismic_gslib_format'};
poro_model_inc_file={'poro_model_5wells','poro_model_explorwell','poro_model_seismic'};
perm_model_inc_file={'perm_model_5wells','perm_model_explorwell','perm_model_seismic'};
Reportq={'Reportq_5wells','Reportq_explorwell','Reportq_seismic'};
q_cases_yearly={'q_5wells_yearly_CMG','q_explorwell_yearly_CMG','q_seismic_yearly_CMG'};
Swav_cases_yearly={'Swav_5wells_yearly_CMG','Swav_explorwell_yearly_CMG','Swav_seismic_yearly_CMG'};

for case_no=1:length(ReservoirModel_CMGBuilder)
    [q_yearly,Swav_yearly]=q_from_CMG_general(nModel,nModel_want,nCell,T,ReservoirModel_CMGBuilder{case_no},poro_models_gslib_file{case_no},perm_models_gslib_file{case_no},poro_model_inc_file{case_no},perm_model_inc_file{case_no},Reportq{case_no});
    save(q_cases_yearly{case_no},'q_yearly');
    save(Swav_cases_yearly{case_no},'q_yearly');
end
+2  A: 

One way to solve this is to assign the variable name with eval first before saving:

for case_no=1:length(ReservoirModel_CMGBuilder)
       [q_yearly,Swav_yearly]=q_from_CMG_general(nModel,nModel_want,nCell,T,ReservoirModel_CMGBuilder{case_no},poro_models_gslib_file{case_no},perm_models_gslib_file{case_no},poro_model_inc_file{case_no},perm_model_inc_file{case_no},Reportq{case_no});
    %# properly name variable...
    eval(sprintf('%s=q_yearly;',q_cases_yearly{case_no});
    %# ... and save it
    save(q_cases_yearly{case_no},q_cases_yearly{case_no});
    %# and do the same thing for the second variable name 
    eval(sprintf('%s=Swav_yearly;',Swav_cases_yearly{case_no});
    save(Swav_cases_yearly{case_no},Swav_cases_yearly{case_no});
end

EDIT

Eval is usually not recommended, since it is difficult to debug/maintain. Thus, you can instead create a structure first and use the -struct-option of save, like this:

for case_no=1:length(ReservoirModel_CMGBuilder)
       [q_yearly,Swav_yearly]=q_from_CMG_general(nModel,nModel_want,nCell,T,ReservoirModel_CMGBuilder{case_no},poro_models_gslib_file{case_no},perm_models_gslib_file{case_no},poro_model_inc_file{case_no},perm_model_inc_file{case_no},Reportq{case_no});
    %# create structure for saving
    saveStruct = struct(q_cases_yearly{case_no},q_yearly,...
        Swav_cases_yearly{case_no},Swav_yearly);
    %# ... and save it
    save(q_cases_yearly{case_no},'-struct','saveStruct',q_cases_yearly{case_no});
    save(Swav_cases_yearly{case_no},'-struct','saveStruct',Swav_cases_yearly{case_no});
end
Jonas
Jonas: `-struct` does exactly as I was asking. Thanks! And regarding the saving the same variable was by mistake :). In the last line it should be `Swav_yearly` instead of `q_yearly`.
Harpreet
@Harpreet: Glad to help. I have fixed the variable names, btw.
Jonas