How to create a file .txt from a matrix?
1 view (last 30 days)
Show older comments
aya ben mabrouk
on 19 Jun 2016
Commented: Muhammad Usman Saleem
on 19 Jun 2016
Hello everyone, I have a matrix x_training (size: 1000*2304) and another matrix y_training (size 1000*1) that contains labels of each instance in x_training. My goal is to create a file .txt that contain labels and x_training. there is a little example for simplification :
x_training=[0.2 0.3
0.5 0.4
0.5 0.9]
y_training=[1
2
3]
so, the file.txt will contain :
1 0.2 0.3
2 0.5 0.4
3 0.5 0.9
Please, help me, How can I do it? and thanks in advance
0 Comments
Accepted Answer
Rime
on 19 Jun 2016
Edited: Rime
on 19 Jun 2016
I think Adam's answer is right. What you need more is a loop:
fid = fopen( 'somefile.txt', 'w' );
for n=1:1000
fprintf( fid, '%d ', y_training(n));
for m=1:2304
fprintf( fid, '%.1f ', x_training(n,m));
end
fprintf(fid,'\n');
end
fclose(fid);
Let me know, if it doesn't work.
0 Comments
More Answers (2)
Adam
on 19 Jun 2016
Edited: Adam
on 19 Jun 2016
fid = fopen( 'somefile.txt', 'w' );
fprintf( fid, '%d %.1f %.1f\n', [y_training x_training ]');
would give what you want. You can change the format specifications to suit what you prefer.
2 Comments
Adam
on 19 Jun 2016
I don't know. What is the problem? I don't have an example matrix of the size you say to test with, but you have obviously tried it so you know better than me what the problem is.
Muhammad Usman Saleem
on 19 Jun 2016
Hi, I hope you are fine
You can create a matrix data of which the first column is of y_training and 2:end ,columns are x_training?
Please try and tell me if not working
data=[y_training x_training(:,1:2304)] %based on your matrix
fId = fopen( 'your file.txt', 'w' ) ;
fprintf( fId, '%i %2.1f %2.1f \r\n', data(:) ) ; % update this line
fclose( fId ) ;
As i have not your matrixes that why here is the demo of testing this code
a=rand(1000,2304); % creating randomly 1000*2304 matrix as x_training
b=rand(1000,1); % creating randomly 1000*1 matrix as y_training
c=[b a(:,1:end)];
data=c;
fId = fopen( 'your file.txt', 'w' ) ;
fprintf( fId, '%i %2.1f %2.1f \r\n', data(:) ) ;
fclose( fId ) ;
sample output text file i obtain
8.985058e-01 0.1 0.3
9.572001e-01 1.0 0.6
7.864674e-01 0.6 0.4
8.036594e-01 1.0 0.6
7.004861e-01 0.3 0.8
2.858565e-01 0.5 0.9
1.721007e-02 0.9 0.9
8.984719e-01 0.9 0.3
3.485380e-01 1.0 1.0
3.902574e-02 0.8 0.4
6.243073e-01 0.8 0.8
1.473974e-01 0.2 0.2
8.838331e-01 0.9 0.5
8.072159e-01 0.4 0.4
1.996930e-01 0.2 0.3
5.608404e-01 0.6 0.3
3.858926e-02 0.0 0.2
9.515419e-01 0.9 0.0
7.501847e-01 0.5 0.4
3.773373e-01 0.1 0.3
6.825213e-01 0.6 0.5
2.274023e-01 0.2 0.3
2.509858e-01 0.4 1.0
2.334426e-01 0.3 0.6
9.447222e-01 0.9 0.7
8.488192e-01 0.2 0.7
3.411729e-01 0.5 0.7
3.744066e-01 0.8 0.3
6.405010e-01 0.4 0.8
7.717461e-01 0.1 0.9
6.768452e-01 0.8 0.8
3.722052e-01 0.3 0.9
1.738441e-01 0.5 0.8
9.710438e-01 0.3 0.3
3.967397e-01 0.6 0.5
2.772546e-01 0.4 0.5
9.171816e-01 0.4 0.8
3.136884e-01 0.6 0.8
5.080256e-01 0.0 0.4
1 Comment
Muhammad Usman Saleem
on 19 Jun 2016
try this also
data=[y_training x_training(:,1:2304)] %based on your matrix
dlmwrite('myFile.txt', data);
Text file size is very long that why can not attached with this comment. Tell me which method is giving you correct output?
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!