Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
 
 
主题工具 显示模式
旧 2019-12-10, 20:42   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 如何将Matlab变量转换为带标题的.dat(文本)文件

编辑的问题:

我在名为avg_data_models的变量中有2500行x 100列数据。我也有2500行x 100列变量'X'和类似大小的矩阵变量'Y',都包含坐标。我想将此变量的值保存在文本(.dat)文件中,该文件必须以以下方式包含302标头行:

avg_data_models 300 X_1 X_2 . . . X_100 Y_1 Y_2 . . . Y_100 avg_data_models_1 avg_data_models_2 avg_data_models_3 . . . . . avg_data_models_100 在上述标题样式中,第一行是文件名,第二行是列数(每列有2500行),其余300行分别代表每个变量的模型-类似于100个模型X,100个Y模型和100个avg_data_models模型。



回答:

考虑以下代码:

%# here you have your data X/Y/.. %#X = rand(2500,100); [rc] = size(X); prefixX = 'X'; prefixY = 'Y'; prefixData = 'avg_data_models'; %# build a cell array that contains all the header lines num = strtrim( cellstr(num2str((1:c)','_%d')) ); %#' SO fix headers = [ prefixData ; num2str(3*c) ; strcat(prefixX,num) ; strcat(prefixY,num) ; strcat(prefixData,num) ]; %# write to file fid = fopen('outputFile.dat', 'wt'); fprintf(fid, '%s\n',headers{:}); fclose(fid); 编辑

看来我误解了这个问题。这是写实际数据的代码(不是标题)!

%# here you have your data X/Y/.. avg_data_models = rand(2500,100); X = rand(2500,100); Y = rand(2500,100); %# create file, and write the title and number of columns fid = fopen('outputFile.dat', 'wt'); fprintf(fid, '%s\n%d\n', 'avg_data_models', 3*size(X,2)); fclose(fid); %# append rest of data dlmwrite('outputFile.dat', [XY avg_data_models], '-append', 'delimiter',',') 注:我用一个逗号,作为分隔符,你可以改变它是一个空间或按\t标签。



更多&回答...
poster 当前离线   回复时引用此帖
 

主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 01:15


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.