开发者

How to convert Matlab variables to .dat (text) file with headers

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_mo开发者_JAVA技巧dels_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.


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..


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜