0

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 !!

Kumar
  • 13
  • 6
  • You save data to file in each iteration, so you don't need to store data as a cell array. Why can't you use the same variable `rainfall` (without {i}) for all iterations: save the current data, and run `rainfall` away the next iteration with the new data, save it too, etc. ? – Adiel Oct 15 '13 at 21:43
  • Without testing I'd say just use `save(Output_path, rainfall_ncep_ncar{i});` without `'` – Robert Seifert Oct 15 '13 at 21:44
  • Not answering your question, but `sprintf('f2/prate.sfc.gauss.%d.mat', i)` instead of strcat would increase the lisibility of your code. By the way, `'rainfall_ncep_ncar{i}'` is not the name of a variable in workspace (`'rainfall_ncep_ncar'` is "the name of" a variable) and still I don't get the point of increasing the complexity by using a cell array since your problem is more about filename... – Pascail Oct 15 '13 at 21:56

1 Answers1

2

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.

user2816823
  • 368
  • 5
  • 11
  • I'm sorry, but your statement that "You can't save individual cells without making another variable." being the problem is wrong. This is not what is happening. The string `'rainfall_ncep_ncar{i}'` is simply not a valid string for `save`. MATLAB doesn't even check for the existence `rainfall_ncep_ncar`. The string is just not a valid variable name. You would get the _exact_ same error for `'fake_nonexistent_cell{i}'`. Go ahead and explain that the cell needs to be extracted first, because that is true, but your interpretation of the error is not quite right. – chappjc Oct 16 '13 at 00:48
  • Sorry, I don't have the option to comment on posts yet :P, if I did I would have. You are right, I did not provide a complete explanation, and I will edit that into my answer. Your answer is not correct because the second argument to save is the string name of the variable, so removing the ' is incorrect. – user2816823 Oct 16 '13 at 01:06
  • I removed my comment because you're explaining it fine now. But note that the first line of code in the loop (`rainfall_ncep_ncar{i} = strcat('rainfall_ncep_ncar', num2str(i));`) defines `rainfall_ncep_ncar` as a cell array *of strings* so the syntax was valid without the '. However, that line of code was misleading since the OP later gives `rainfall_ncep_ncar{i}=netcdf.getVar(ncid,prateId);` in the explanation of the problem below the main code. For this data, you are right that it would not be a string and it is necessary to pull out the cell contents into another variable first. And +1! :) – chappjc Oct 16 '13 at 03:12