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

9 visualizaciones (últimos 30 días)
Mr. 206
Mr. 206 el 15 de Nov. de 2018
Comentada: Guillaume el 20 de Nov. de 2018
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 comentario
Bob Thompson
Bob Thompson el 15 de Nov. de 2018
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.

Iniciar sesión para comentar.

Respuestas (2)

Steven Lord
Steven Lord el 15 de Nov. de 2018
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 comentarios
Stephen23
Stephen23 el 16 de Nov. de 2018
"Actually i need them in txt file."
That is exactly what writetable does: it writes a text file.
Guillaume
Guillaume el 20 de Nov. de 2018
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);

Iniciar sesión para comentar.


Jan
Jan el 16 de Nov. de 2018
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 comentarios
Mr. 206
Mr. 206 el 20 de Nov. de 2018
Actually i have to do it in this way!
Guillaume
Guillaume el 20 de Nov. de 2018
"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?

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by