Effacer les filtres
Effacer les filtres

How do I save a matrix in ascii format so that the variable names are included as the first row header?

8 vues (au cours des 30 derniers jours)
Hi,
I have assigned variable names to each column (14 columns) of my matrix. But when I save the matrix in ascii format, the variable names are not included in the saved txt file. How can I save the matrix in a txt file so that the variable names are included as the first row header?

Réponses (1)

Guillaume
Guillaume le 7 Avr 2017
"I have assigned variable names to each column of my matrix." This is not something that can be done in matlab. Matrices can only contain numbers, not strings. So, you've either converted your matrix into a cell array, split the matrix into individual variables (not a good idea at all!) or are using a table, which has explicit support for naming columns.
That last option (table) is the easiest way for you to achieve what you want. If you have not done so, convert the matrix to a table with array2table, then use writetable to save to disk. By default, writetable writes the column names as a header, so there's nothing special to do in your case.:
%yourmatrix: the matrix to write to text file
%columnnames: cell array of column names, e.g.:
yourmatrix = randi(100, 10, 4); %10x4 matrix for demo
columnnames = {'Pressure', 'Temperature', 'Volume', 'Heat Release'};
writetable(array2table(yourmatrix, 'VariableNames', columnnames), 'somefile.txt');
  2 commentaires
Michel Nieuwoudt
Michel Nieuwoudt le 7 Avr 2017
Thanks Guillaume, it worked a treat!
best wishes, Michel
Jan
Jan le 7 Avr 2017
Modifié(e) : Jan le 7 Avr 2017
@Michel: If the answer solves your problem, you can accept it. Then the voluntary readers in the forum see, that this question does not need an answer anymore and they can focus other questions. Thanks.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Function Creation 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!

Translated by