Writing a matrix with header into a .csv file

590 ビュー (過去 30 日間)
Happy gree
Happy gree 2016 年 4 月 26 日
コメント済み: Vishnu Sankar 2022 年 4 月 27 日
I have this matrix:
A=(1 2 3;4 5 6)
I want to add headline to the matrix as the output is in .csv file. This is what I have tried so far.
filename= uigetfile('*.csv','choose data file');
title ={'x-axis' ,'y-axis','z-axis'};
if exist('filename');
csvwrite('result.csv',title,A);
end
The output should look like this in csv:
|x-axis|y-axis|z-axis|
|1 |2 |3 |
|4 |5 |6 |
This is supposed to be a lookalike of excel cell :) I'd be very grateful if someone could recommend a way to figure this out.
Regard, Happy
  3 件のコメント
Happy gree
Happy gree 2016 年 4 月 26 日
Hi, thank you for trying to help me.
I have inserted the code you suggested now I don't know how to input this into .csv file. It shows what I want as an outcome in the workspace area.
Regard, Happy
jgg
jgg 2016 年 4 月 27 日
As I suggested, use the writetable command.
You can read about that command using doc writetable

サインインしてコメントする。

回答 (1 件)

Navdha Agarwal
Navdha Agarwal 2019 年 6 月 20 日
- If you do not want the titles or the name of the columns then you can use:
A=[1 2 3;4 5 6]
csvwrite('file.csv',A)
- If you want to specify the name of the titles as well then you can first convert the matrix into the table and then write the table to the csv file.
A=[1 2 3;4 5 6]
T = array2table(A)
T.Properties.VariableNames(1:3) = {'x_axis','y_axis','z_axis'}
writetable(T,'file1.csv')
  1 件のコメント
Vishnu Sankar
Vishnu Sankar 2022 年 4 月 27 日
Thanks a lot!!! Exactly what I was looking for

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by