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

31 views (last 30 days)
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?

Answers (1)

Guillaume
Guillaume on 7 Apr 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 Comments
Jan
Jan on 7 Apr 2017
Edited: Jan on 7 Apr 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.

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!