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

30 次查看(过去 30 天)
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?

回答(1 个)

Guillaume
Guillaume 2017-4-7
"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 个评论
Jan
Jan 2017-4-7
编辑:Jan 2017-4-7
@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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by