How to store matrix in a txt file with Row and Column name?
    5 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
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
      
 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.
Respuestas (2)
  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
      
      
 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
      
      
 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);
  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
  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?
Ver también
Categorías
				Más información sobre Standard File Formats en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




