How to store matrix in a txt file with Row and Column name?
    5 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
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 commentaire
  Bob Thompson
      
 le 15 Nov 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.
Réponses (2)
  Steven Lord
    
      
 le 15 Nov 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 commentaires
  Stephen23
      
      
 le 16 Nov 2018
				"Actually i need them in txt file."
That is exactly what writetable does: it writes a text file.
  Guillaume
      
      
 le 20 Nov 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
      
      
 le 16 Nov 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 commentaires
  Guillaume
      
      
 le 20 Nov 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?
Voir également
Catégories
				En savoir plus sur Standard File Formats dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




