2015-04-30 114 views
0

我已经创建了一个脚本来在我的工作区中创建~300个变量名。我需要将这些.mat文件保存为.txt文件 - 有什么方法可以在某种循环中调用这些变量,或者将它们一次全部保存为.txt文件?将多个.mat文件从工作区保存到.txt文件

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

这里是.MAT的一个示例文件 -

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

我不知道一大堆有关Matlab的所以任何帮助将非常感激!谢谢。

+1

您需要说明这些.txt文件需要什么。他们是否必须与另一个程序进行交互?他们是否应该采用特定的格式?你打算在未来将它读回Matlab吗? – Setsu

+1

为什么一个文本文件? – siliconwafer

+0

对此缺乏澄清感到抱歉!我需要将.txt文件作为与.mat文件具有相同尺寸的表格导入到Mathematica中 –

回答

0

一种可能的解决方案可以是:

.mat文件通过一个循环装载(该文件的名可以是动态地从一个列表中生成或读)

提取的第一行cellarray(字符串),并通过使用dlmwrite函数

以去除所述第一行和(与dlmwrite再次)写的数值在具有.txt它写在一个.txt文件转换他们使用cell2mat函数。在写入数值时,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