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

9 次查看(过去 30 天)
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 个评论
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?

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by