title: Saving matlab files with a name having a variable input
body: <p>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.  </p>
  
  <pre><code> %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
  </code></pre>
  
  <p>When I am trying to use </p>
  
  <pre><code>    rainfall_ncep_ncar{i}=netcdf.getVar(ncid,prateId);
      save(Output_path, 'rainfall_ncep_ncar{i}');
  </code></pre>
  
  <p>In place of </p>
  
  <pre><code>    rainfall = netcdf.getVar(ncid,prateId);
      save(Output_path, 'rainfall');  
  </code></pre>
  
  <p>It shows following error</p>
  
  <pre><code>     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 ';']);
  </code></pre>
  
  <p>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??</p>
  
  <p>Thanks in advance !!</p>