Inputting Date Time data and presenting as a graph
显示 更早的评论
A = importdata('08_underwater.txt')
A = readtimetable('08_underwater.txt')
A.Properties.VariableNames{1} = 'Light';
A.Time.Format='M/dd/yyyy HH:mm'
plot(A.Time,A.Light)
^ with this the x axis appears totally wrong as I am plotting data for only 01/12/21 - 03/12/21
Light is correctly displayed on the y axis
I would like to show light changes over time over the three days
I am wondering if the x axis requires more formatting?
回答(1 个)
A = readtimetable('08_underwater.csv')
A.Properties.VariableNames{1} = 'Light';
plot(A.Datetime,A.Light)
29 个评论
Sophia
2021-12-7
Chunru
2021-12-7
Use "readtimetable" as shown above.
Chunru
2021-12-7
Or change Time to 'Datetime' (with quotes).
Sophia
2021-12-7
Chunru
2021-12-7
You need to initialize opts before setvaropts:
opts = detectImportOptions('08_underwater.csv');
Have you used a different data file from what you uploaded?
Sophia
2021-12-7
Chunru
2021-12-7
Post your complete code.
Sophia
2021-12-7
Chunru
2021-12-7
Your code please. (.m file not .mat file)
Sophia
2021-12-7
% You just need one of the two "read" to read the data
%
% The following returns a cell array. [Not recommended as you
% need to convert the format]
% A = importdata('08_underwater.csv')
% The following read in data as table.
opts = detectImportOptions('08_underwater.csv');
opts = setvaropts(opts, 'Datetime', 'InputFormat', 'dd/MM/uuuu HH:mm:ss');
type 08_underwater.csv
A = readtable('08_underwater.csv', opts)
% You don't need the following as the default above works well.
%
% opts = detectImportOptions('08_underwater.csv');
% opts = setvaropts(opts,'Datetime','InputFormat','M/dd/yyyy HH:mm');
plot(A.Datetime,A.Light)
Sophia
2021-12-7
Chunru
2021-12-7
You should have pointed it out earlier. Corrected above.
Sophia
2021-12-7
Sophia
2021-12-12
Chunru
2021-12-12
All the code and data are in the post. The code is run on line. I have nothing else to share. Show how you run the code what is the error.
Sophia
2021-12-12
Chunru
2021-12-12
What is your matlab version?
Sophia
2021-12-12
Run the following and show the output and error message
clear all
opts = detectImportOptions('08_underwater.csv')
opts = setvaropts(opts, 'Datetime', 'InputFormat', 'dd/MM/uuuu HH:mm:ss')
A = readtable('08_underwater.csv', opts)
plot(A.Datetime,A.Light)
Sophia
2021-12-12
Chunru
2021-12-12
Try the following:
A = readtable('08_underwater.csv', 'DatetimeType', 'text')
A.Datetime = datetime(A.Datetime, 'InputFormat', 'dd/MM/yyyy HH:mm:ss')
plot(A.Datetime,A.Light)
Sophia
2021-12-12
You are using a different data file from what you posted. The datetime format uses '-' instead of '/'. Change that and try again.
datetime('3-12-2021 17:45', 'InputFormat', 'dd-MM-yyyy HH:mm:ss')
Sophia
2021-12-12
Chunru
2021-12-13
Post your data.
Sophia
2021-12-13
Use the same data and code here.
opts = detectImportOptions('08_underwater.csv');
opts = setvaropts(opts, 'Datetime', 'InputFormat', 'dd/MM/uuuu HH:mm');
% type 08_underwater.csv
A = readtable('08_underwater.csv', opts);
plot(A.Datetime,A.Light)
Sophia
2021-12-13
类别
在 帮助中心 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


