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?
0 个评论
回答(1 个)
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!