![]() |
如何将Matlab变量转换为带标题的.dat(文本)文件
[B]编辑的问题:[/B]
我在名为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模型。 [B]回答:[/B] 考虑以下代码: %# 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); [B]编辑[/B] 看来我误解了这个问题。这是写实际数据的代码(不是标题)! %# 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标签。 [url=https://stackoverflow.com/questions/3172729]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 01:03。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.