保存名称具有可变输入名称的matlab文件 [英] Saving matlab files with a name having a variable input

查看:567
本文介绍了保存名称具有可变输入名称的matlab文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,我是matlab的新手.我想出了一个代码,可以将所有nc文件转换为mat文件.我用了一个for循环.一切都很好,我能够成功转换所有文件.但是有一个小缺点.所有文件都具有相同的变量名(出现在工作区中).这需要手动重命名.我认为这是由于我的matlab语法限制所致.我把代码放在下面.如果您可以提出一种建议,那将是很棒的.修复后,对于任何人来说,这实际上都是节省时间的代码.

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

当我尝试使用

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

代替

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

它显示以下错误

     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 ';']);

我想将每个文件保存为f2/prate.sfc.gauss.1948.mat和工作区中对应的变量prate.sfc.gauss.1948或1948或具有年份的东西.我该怎么办??

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

预先感谢!

推荐答案

出现此错误的原因很简单-"'rainfall_ncep_ncar{i}' is not a valid variable name."如果不创建另一个变量,您将无法保存单个单元格. 编辑:rainfall_ncep_ncar是工作区中的变量,但rainfall_ncep_ncar{i}不是,因此对于保存的第二个参数而言不是有效的字符串.必须先将单元格提取到另一个变量(例如rainfall)中,然后才能保存它.

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

还有其他几点.

  1. 当您调用rainfall_ncep_ncar= cell(1948, 2012);时,您将在2012年前制作一个大小为1948的2D单元阵列.从该单元阵列表示的内容来看,这绝对不是您想要的!您想使用cell生成一个2012年至1948年的1尺寸单元格数组.

  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.

为什么要将每个单元格保存在不同的mat文件中?在大多数情况下,如果以后要使用它,最好在循环末尾将单元格数组保存在一个mat文件中,这样可能会更好.实际上,在这种情况下,没有使用单元数组的真正理由!

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!

希望有帮助.

这篇关于保存名称具有可变输入名称的matlab文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆