如何将 Matlab 变量转换为带标题的 .dat(文本)文件

发布于 2024-09-07 22:52:52 字数 463 浏览 2 评论 0原文

编辑问题:

我在名为 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 模型。

EDITED QUESTION:

I have 2500 rows x 100 columns data in variable named avg_data_models. I also have 2500 rows x 100 columns variable 'X' and similar size matrix variable 'Y', both containing the co-ordinates. I want to save the values of this variable in a text (.dat) file which must have 302 header lines in the following manner:

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

In the above header style, the first line is the name of the file, the 2nd line tells the number of columns (each column has 2500 rows), and the rest of the 300 lines represent the model of each variable respectively - Like 100 models of X, 100 models of Y and 100 models of avg_data_models.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假情假意假温柔 2024-09-14 22:52:52

考虑这段代码:

%# here you have your data X/Y/..
%#X = rand(2500,100);

[r c] = 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', [X Y avg_data_models], '-append', 'delimiter',',')

注意:我使用了逗号 ,作为分隔符,如果您愿意,您可以将其更改为空格 或制表符 \t

Consider this code:

%# here you have your data X/Y/..
%#X = rand(2500,100);

[r c] = 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);

EDIT

It seems I misunderstood the question.. Here's the code to write the actual data (not the header titles!):

%# 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', [X Y avg_data_models], '-append', 'delimiter',',')

Note: I used a comma , as delimiter, you can change it to be a space or a tab \t if you like..

您可以使用 fprintf 来编写标头,如下所示:

%# define the number of data
nModels = 100;
dataName = 'avg_data_models';

%# open the file
fid = fopen('output.dat','w');

%# start writing. First line: title
fprintf(fid,'%s\n',dataName); %# don't forget \n for newline. Use \n\r if yow want to open this in notepad

%# write number of models
fprintf(fid,'%i\n',nModels)

%# loop to write the rest of the header
for iModel = 1:nModels
fprintf(fid,'%s_%i\n',dataName,iModel);
end

%# use your favorite method to write the rest of the data. 
%# for example, you could use fprintf again, using /t to add tabs 
%# create format-string
%# check the help to fprintf to learn about formatting details
formatString = repmat('%f\t',1,100);
formatString = [formatString(1:end-1),'n']; %# replace last tab with newline

%# transpose the array, because fprintf reshapes the array to a vector and 
%# 'fills' the format-strings sequentially until it runs out of data
fprintf(fid,formatString,avg_data'); %'# SO formatting

%# close the file
fclose(fid);

You can use fprintf to write the header, like so:

%# define the number of data
nModels = 100;
dataName = 'avg_data_models';

%# open the file
fid = fopen('output.dat','w');

%# start writing. First line: title
fprintf(fid,'%s\n',dataName); %# don't forget \n for newline. Use \n\r if yow want to open this in notepad

%# write number of models
fprintf(fid,'%i\n',nModels)

%# loop to write the rest of the header
for iModel = 1:nModels
fprintf(fid,'%s_%i\n',dataName,iModel);
end

%# use your favorite method to write the rest of the data. 
%# for example, you could use fprintf again, using /t to add tabs 
%# create format-string
%# check the help to fprintf to learn about formatting details
formatString = repmat('%f\t',1,100);
formatString = [formatString(1:end-1),'n']; %# replace last tab with newline

%# transpose the array, because fprintf reshapes the array to a vector and 
%# 'fills' the format-strings sequentially until it runs out of data
fprintf(fid,formatString,avg_data'); %'# SO formatting

%# close the file
fclose(fid);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文