How to store matrix in a txt file with Row and Column name?

조회 수: 15 (최근 30일)
Mr. 206
Mr. 206 2018년 11월 15일
댓글: Guillaume 2018년 11월 20일
Here is my Code that is storing a 12×N matrix named "MEEM_orig"
MEEM_Orig_File = '/home/directory'; % Assiging directory
[fid,msg] = fopen(fullfile(MEEM_Orig_File, 'MEEM_Orig_File.txt'),'wt'); % Opening a txt file
MEEM_orig; % Checking whether the right matrix is going to the process
model_number = 1:size(MEEM_orig,1); % Will be used to name Rows
fputs (fid, "MEEM_Orig\n\n"); % just creating a heading
fprintf(fid,'"M%d" %.16f %.16f %.16f\n',[model_number(:),MEEM_orig].') % I am basically stuck in this line
fclose (fid);
A sample Matrix is provided in the attached file for testing.
How can i store the Matrix MEEM_orig (12×N) by naming the Rows like "M01", "M02", "M03"........................."M12". And if a column vector provides (the vector is not in this code) data like column_name = [37.txt, 38.txt, 39.txt...............................................................at the end of the columns], How can i use it for naming each column in that matrix like "37.txt", "38.txt".............. and so on.
  댓글 수: 1
Bob Thompson
Bob Thompson 2018년 11월 15일
Does it need to be a .txt file specifically? This would be relatively easy with a .csv file, which can still be opened in a text editor if you require.

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

답변 (2개)

Steven Lord
Steven Lord 2018년 11월 15일
Consider storing your data in a table array instead of a matrix and two text variables containing row and column names. If you do, you can use writetable to write the table to a text file.
  댓글 수: 3
Stephen23
Stephen23 2018년 11월 16일
"Actually i need them in txt file."
That is exactly what writetable does: it writes a text file.
Guillaume
Guillaume 2018년 11월 20일
So, using writetable it could be as simple as:
t = array2table(MEEM_orig);
t.RowNames = compose('M%02d', 1:height(t));
t.VariableNames = compose('C%02d', 1:width(t)); %I did not understand the naming of the columns
writetable(t, fullfile(MEEM_Orig_File, 'MEEM_Orig_File.txt'), 'WriteRowNames', true);

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


Jan
Jan 2018년 11월 16일
Your code looks almost fine. So what is the problem withit?
MEEM_Orig_Path = '/home/directory'; % Assiging directory
[fid,msg] = fopen(fullfile(MEEM_Orig_Path, 'MEEM_Orig_File.txt'),'wt'); % Opening a txt file
% Useless: MEEM_orig; % This does not do anything
model_number = 1:size(MEEM_orig,1); % Will be used to name Rows
fputs (fid, "MEEM_Orig\n\n"); % just creating a heading
fmt = ['"M%d', repmat('%.16f', size(MEEM_orig, 2)), '\n'];
fprintf(fid, fmt, [model_number(:), MEEM_orig].') % I am basically stuck in this line
fclose (fid);
The part "And if a column vector provides (the vector is not in this code) data like column_name = [37.txt, 38.txt, 39.txt..." is not clear. Maybe you mean a cell string:
column_name = {'37.txt', '38.txt', '39.txt', ... expand as needed
Then add this before the "fclose(fid)" line:
fprintf(fid, ' ');
fprintf(fid, '%18s', column_name{:}}); % Adjust the width to your needs
fprintf(fid, '\n');
  댓글 수: 5
Mr. 206
Mr. 206 2018년 11월 20일
Actually i have to do it in this way!
Guillaume
Guillaume 2018년 11월 20일
"Actually i have to do it in this way! "
You are certainly free to make your life more complicated than it needs to, but why?

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by