【问题标题】:Saving multiple .mat files from workspace to .txt files将多个 .mat 文件从工作区保存到 .txt 文件
【发布时间】:2015-04-30 17:15:45
【问题描述】:

我创建了一个脚本来在我的工作区中创建约 300 个变量名。我需要将这些 .mat 文件保存到 .txt 文件中 - 有没有办法在某种循环中调用这些变量,或者一次将它们全部保存到 .txt 文件中?

它们都是不同大小的单元格,平均约为 1000x5。 第一行数据是字符串,其他元素都是非整数。

这是 .mat 文件之一的示例-

http://www.yourfilelink.com/get.php?fid=1065782

我对 Matlab 了解不多,因此非常感谢任何帮助!谢谢。

【问题讨论】:

  • 您需要用这些 .txt 文件阐明您需要什么。他们必须与另一个程序互操作吗?它们应该采用特定格式吗?你以后打算把它读回 Matlab 吗?
  • 抱歉没有说明清楚!我需要将 .txt 文件作为与 .mat 文件相同尺寸的表格导入 Mathematica

标签: matlab


【解决方案1】:

一个可能的解决方案是:

通过循环加载.matfiles(文件名可以动态生成或从列表中读取)

提取单元格数组的第一行(字符串)并使用dlmwrite 函数将其写入.txt 文件

删除第一行并写入(再次使用dlmwrite)在使用cell2mat 函数转换后的.txt 中的数值。在写入数值时,precision 必须小心设置。

file_name_root='x2413';
% dummy loop (one iteration) just to test dynamic filename generation
for i=3:3
   % generate the name of the input file
   input_file_name=[file_name_root int2str(i)];
   % generate the name of the output file
   output_file_name=[file_name_root int2str(i) '.txt'];
   % extract the name of the cellarray and assign it to "the_cell" variable
   tmp=load(input_file_name);
   var_name=char(fieldnames(tmp));
   eval(['the_cell=tmp.' var_name ';'])
   [r,c]=size(the_cell);
   str='';
   % get the first row string (probably a cleaver way to do it exists)
   for i=1:c
      str=[str ' ' char(the_cell(1,i))];
   end
   % print the first row in a ".txt" file
   dlmwrite(output_file_name,sprintf('%s',str),'delimiter','')
   % remove the first row
   the_cell(1,:)=[];
   % write the numerical values in the ".txt" file
   % the "precision" shall be carefully set
   dlmwrite(output_file_name,cell2mat(the_cell),'precision','%13.9f','delimiter',' ','-append')
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多