一种可能的解决方案可以是:
到.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
您需要说明这些.txt文件需要什么。他们是否必须与另一个程序进行交互?他们是否应该采用特定的格式?你打算在未来将它读回Matlab吗? – Setsu
为什么一个文本文件? – siliconwafer
对此缺乏澄清感到抱歉!我需要将.txt文件作为与.mat文件具有相同尺寸的表格导入到Mathematica中 –