plot csv with column names used automatically as x and y label

This is the data.
DateTime,Number
2024-11-18 03:37:01,0
2024-11-18 03:38:01,0
2024-11-18 03:39:01,200
2024-11-18 03:40:01,400
2024-11-18 03:41:01,600
2024-11-18 03:42:01,600
2024-11-18 03:43:01,800
2024-11-18 03:44:01,1000
2024-11-18 03:45:01,1200
This is the script.
data = readtable("results.csv","TextType","string");
plot(data.DateTime, data.Number);
I want to use DateTime for xlabel and Number for ylabel automatically.

 採用された回答

Walter Roberson
Walter Roberson 2024 年 11 月 20 日
編集済み: Walter Roberson 2024 年 11 月 20 日

0 投票

data = readtable("results.csv","TextType","string");
plot(data, data.Properties.VariableNames(1), data.Properties.VariableNames(2));

5 件のコメント

Igenyar
Igenyar 2024 年 11 月 21 日
Thanks, below is the error.
>> data.Properties.VariableNames(2)
ans =
1×1 cell array
{'Number'}
>> data.Properties.VariableNames(1)
ans =
1×1 cell array
{'DateTime'}
>> plot(data,data.Properties.VariableNames(1), data.Properties.VariableNames(2));
Error using tabular/plot
Too many input arguments.
Walter Roberson
Walter Roberson 2024 年 11 月 21 日
data = readtable("results.csv","TextType","string");
plot(data, data.Properties.VariableNames{1}, data.Properties.VariableNames{2});
Igenyar
Igenyar 2024 年 11 月 21 日
編集済み: Igenyar 2024 年 11 月 21 日
Sorry, I still see the same error.
>> data = readtable("results.csv","TextType","string");
plot(data, data.Properties.VariableNames{1}, data.Properties.VariableNames{2});
Error using tabular/plot
Too many input arguments.
>> data
data =
1439×2 table
DateTime Number
___________________ ______
2024-11-19 00:01:18 0
2024-11-19 00:02:18 133
2024-11-19 00:03:18 383
2024-11-19 00:04:18 511
2024-11-19 00:05:18 600
...
>> version
ans =
'9.7.0.1190202 (R2019b)'
Walter Roberson
Walter Roberson 2024 年 11 月 21 日
Sigh, you should have mentioned earlier that you were using an old release.
data = readtable("results.csv","TextType","string");
plot(data{:,1}, data{:,2});
xlabel(data.Properties.VariableNames{1});
ylabel(data.Properties.VariableNames{2});
Igenyar
Igenyar 2024 年 11 月 21 日
Thank you!

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

その他の回答 (1 件)

Rahul
Rahul 2024 年 11 月 21 日
編集済み: Rahul 2024 年 11 月 21 日
Hi Igenyar,
I believe you want to plot the two columns ‘DateTime’ and ‘Number’ of the given csv file ‘results.csv’, while using ‘DateTime’ for ‘xlabel’ and ‘Number’ for ‘ylabel’ respectively in a plot.
Based on the sample data provided, I am able to reproduce this issue on MATLAB R2024b, and the given script provides the expected plots.
I used the given sample table for creating a local csv file, in order to verify the provided script. The axes labels have been specified as variables names in data file ‘results.csv’. Here’s how the output looks like:
  • Generating sample csv file:
% Define the data
DateTime = {'2024-11-18 03:37:01'; '2024-11-18 03:38:01'; '2024-11-18 03:39:01';
'2024-11-18 03:40:01'; '2024-11-18 03:41:01'; '2024-11-18 03:42:01';
'2024-11-18 03:43:01'; '2024-11-18 03:44:01'; '2024-11-18 03:45:01'};
Number = [0; 0; 200; 400; 600; 600; 800; 1000; 1200];
% Convert DateTime strings to datetime objects
DateTime = datetime(DateTime, 'InputFormat', 'yyyy-MM-dd HH:mm:ss');
% Create the table
dataTable = table(DateTime, Number);
% Write the table to a CSV file
writetable(dataTable, 'data.csv');
  • Plotting column data on appropriate axes:
data = readtable("data.csv","TextType","string");
plot(data.DateTime, data.Number);
xlabel(data.Properties.VariableNames{2});
ylabel(data.Properties.VariableNames{1});
To know more about the usage of ‘plot’ function, you can use the following command in MATLAB:
>> doc plot
Regards!

2 件のコメント

Igenyar
Igenyar 2024 年 11 月 21 日
I accept @Walter Roberson's answer, but your answer is helpful too. Thank you!
Walter Roberson
Walter Roberson 2024 年 11 月 21 日
plot(data.DateTime, data.Number);
That assumes column labels, but the premise of the question was that the column labels are variable.

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

カテゴリ

ヘルプ センター および File Exchange で Time Series Events についてさらに検索

製品

リリース

R2019b

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by