Reputation: 65
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
. Here 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
How can I do it?
Upvotes: 2
Views: 226
Reputation: 149
FINAL EDIT
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
x_training=rand(1000,2304); % creating randomly 1000*2304 matrix as x_training
y_training=rand(1000,1); % creating randomly 1000*1 matrix as y_training
test=[y_training x_training(:,1:end)];
data=test;
fId = fopen( 'your file.txt', 'w' ) ;
fprintf( fId, '%i %2.1f %2.1f \r\n', data(:) ) ;
fclose( fId ) ;
Alternate method:
if above code not give desire output, update this
data=[y_training x_training(:,1:2304)] %based on your matrix
dlmwrite('myFile.txt', data);
Check it and tell me which one is fine code.
Upvotes: 1
Reputation: 1492
Here is an alternative method
dlmwrite('testtext2.txt',['x_training y_training'] ,'delimiter','')
A(:,1) = y_training
A(:,2:3) = x_training
dlmwrite('testtext2.txt',[A],'-append','delimiter','\t','roffset',1)
Upvotes: 1
Reputation: 2027
Its simple. Create a file (lets say OutPutFile.txt) in your MATLAB Current folder and run this script:
A = [y_training x_training];
B = A';
fileID = fopen('OutPutFile.txt','w');
fprintf(fileID, '%i %2.1f %2.1f \n',B);
fclose(fileID);
If you want to make a Tab-separated Values (TSV) file (which is openable in Excel), then modify the 2nd line to:
fprintf(fileID, '%i \t %2.1f \t %2.1f \n', B);
Upvotes: 3
Reputation: 259
x_training=[0.2 0.3; 0.5 0.4; 0.5 0.9]; % X
y_training=([1 2 3])'; % Y - transposed
data = [y_training,x_training]; % x and y concatenated
data = table(data); % data converted to table
writetable(data, 'file.txt'); % data saved as file.txt
Upvotes: 3