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!


