Search code examples
matlabvariablesevalcell

Saving matlab files with a name having a variable input


Hi friends I am new to matlab. I came up with a code which can convert all nc files to mat files in one turn. I used a for loop. Everything is fine and I am able to convert all files successfully. But there is a small drawback. All files have same variable name(which appears in workspace). This requires manual renaming. I think this is due to my matlab syntax limitation. I am putting code below. It will be great if you can suggest a way. After fixing, it will be really time saving code for anyone.

 %Author--  
 %converting nc file to mat file

 % Start_year = 1948;
 % End_year = 2012;
 rainfall_ncep_ncar= cell(1948, 2012);
  clear 
 for i=1948 : 2012
    % inputfile = strcat('prate.sfc.gauss.', num2str(i),'.nc');
    % disp(inputfile);
     rainfall_ncep_ncar{i} = strcat('rainfall_ncep_ncar', num2str(i));
    % disp(rainfall_ncep_ncar_{i});

    % disp(outfile);
    % disp(year);
    %clear other existing variables 
    %Output_filename = '../NCER_precipitation_rate_mat/rainfall_data_' +year;
    % check ='../NCER_precipitation_rate_mat/'inputfile;
     Input_path =strcat('../NCEP_precipitation_rate_nc/prate.sfc.gauss.',   num2str(i),'.nc');
    %display(Input_path);
    ncid = netcdf.open(Input_path, 'NC_NOWRITE');
   try
      prateId = netcdf.inqVarID(ncid, 'prate');
   catch exception 
      if strcmp(exception.identifier,'MATLAB:imagesci:netcdf:libraryFailure')
           str = 'prateId not found';
            end
   end 

   %disp(rainfall_ncep_ncar{i});
   rainfall = netcdf.getVar(ncid,prateId);
    %rainfall{i}= netcdf.getVar(ncid,prateId);
    Output_file = strcat('rainfall_ncep_ncar_', num2str(i),'.mat');
   %disp(Output_file);

   Output_path = strcat('f2/prate.sfc.gauss.', num2str(i),'.mat');
   save(Output_path, 'rainfall');
   disp(Output_path);
   disp('done');
   netcdf.close(ncid);
end
  clear

When I am trying to use

    rainfall_ncep_ncar{i}=netcdf.getVar(ncid,prateId);
    save(Output_path, 'rainfall_ncep_ncar{i}');

In place of

    rainfall = netcdf.getVar(ncid,prateId);
    save(Output_path, 'rainfall');  

It shows following error

     run('H:\btp\mexnc files\nc_to_mat_all.m')
      Error using save
         'rainfall_ncep_ncar{i}' is not a valid variable name.

      Error in nc_to_mat_all (line 40)
         save(Output_path, 'rainfall_ncep_ncar{i}');

      Error in run (line 57)
         evalin('caller', [s ';']);

I want to save each file like f2/prate.sfc.gauss.1948.mat and the corresponding variable which comes in workspace as prate.sfc.gauss.1948 or 1948 or something having year. How do I do it??

Thanks in advance !!


Solution

  • The reason why that error is popping up is simple - "'rainfall_ncep_ncar{i}' is not a valid variable name." You can't save individual cells without making another variable. EDIT: rainfall_ncep_ncar is a variable in the workspace, but rainfall_ncep_ncar{i} is not, so it is not a valid string for the second parameter of save. The cell needs to be extracted first into another variable (such as rainfall) before it can be saved.

    Also a few other points.

    1. When you call rainfall_ncep_ncar= cell(1948, 2012);, you are making a 2D cell array of size 1948 by 2012. Judging by what that cell array represents, this is definitely not what you want! You want to generate a 2012 - 1948 by 1 size cell array using cell.

    2. Why are you saving each cell in a different mat file? You would probably be better served in most cases saving the cell array at the end of the loop in one mat file if you are going to use it later. In fact, in this case there is no real reason to use a cell array!

    Hope that helps.