필터 지우기
필터 지우기

How to create a file .txt from a matrix?

조회 수: 5 (최근 30일)
aya ben mabrouk
aya ben mabrouk 2016년 6월 19일
댓글: Muhammad Usman Saleem 2016년 6월 19일
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

채택된 답변

Rime
Rime 2016년 6월 19일
편집: Rime 2016년 6월 19일
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.

추가 답변 (2개)

Adam
Adam 2016년 6월 19일
편집: Adam 2016년 6월 19일
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
aya ben mabrouk
aya ben mabrouk 2016년 6월 19일
it works for the little example, but as I said in the question, I work with matrix with size 1000*2304 et 1000*1. it dosen't work in this case. what is the problem?
Adam
Adam 2016년 6월 19일
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
Muhammad Usman Saleem 2016년 6월 19일
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
Muhammad Usman Saleem
Muhammad Usman Saleem 2016년 6월 19일
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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by