How can I plot dates?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hello,
How can I plot date in x-axis?
My csv file import dates as: '03/27/2020 19:10:35.537'
My code:
D = data{:,2};
d = datenum(D);
date = datetime(d,'Format','dd-MM-yyyy HH:mm:ss.SS','convertFrom','datenum');
As a result, I have my plots good, but only in same data (see Figure).
All my data has the same time, and have the same format 805 x 1 (double). For example, yellow line(good) and blue line (wrong) should be the same on x-axis.

채택된 답변
Cris LaPierre
2020년 4월 11일
0 개 추천
I think we need to see your plot command to say for certain what is happening.
My recommendation would be to have your X data be the datetime variable. MATLAB is able to use that as X, and will may the XTickLabels the dates, which are easier to understand.
I would therefore use readtable to load the csv file. That way, I can import the dates directly as datetimes.
댓글 수: 8
Márcio Gonçalves
2020년 4월 11일
편집: Cris LaPierre
2020년 4월 11일
Thanks for answer.
I am already import with readtable. However i have an error with plotting datetimes directly
Error using plot
Invalid data argument.
Error in hygrothermal_cyclesv2 (line 159)
plot(date, T1D,'b--','LineWidth',1);
Please see my code:
data = readtable('test1.csv');
D = data{:,2}; % Data
d = datenum(D);
date = datetime(d,'Format','dd-MM-yyyy HH:mm:ss.SS','convertFrom','datenum');
%termopares
T1D = (data{:,3}); % termopar 1D - canal 101
T2D = (data{:,4}); % termopar 2D - canal 102
T3D = (data{:,5}); % termopar 3D - canal 103
T4D = (data{:,6}); % termopar 4D - canal 104
...
%----------------------------------------------------------------------
figure()
set(gcf,'color','w');
set(gca,'fontsize', 18)
plot(date, T1D,'b--','LineWidth',1);
hold on
plot(date, T2D,'b','LineWidth',1);
hold on
plot(date, T3D,'g--','LineWidth',1);
hold on
plot(date, T4D,'g','LineWidth',1);
hold on
...
plot(inside,'k--','LineWidth',1);
hold on
plot(outside,'k:','LineWidth',1);
hold on
datetick('x','HH:MM:SS');
xlabel(('Date'), 'FontSize',18)
ylabel(('Temperature [ºC]'),'FontSize',18);
legend('T1D','T2D', 'T3D', 'T4D','T5D', 'T6D','T7D','T8D','T9D','T10D','inside','outside','Location','eastoutside','FontSize',18);
title('Layer D','FontSize',20);
Cris LaPierre
2020년 4월 11일
Any chance you can share your file test1.csv? If so, please attach it to your post.
In the meantime, you only need to turn hold on once for an axes. Once you are done plotting to that axes, it's also best practice to then turn it off.
Cris LaPierre
2020년 4월 11일
In the meantime, I mocked up a simple example using the data format you mentioned. Here is how I might do it. Replace 'Var#' with whatever the variable names are in your table.
data = readtable('test1.csv');
data.Var2.Format = 'dd-MM-yyyy HH:mm:ss.SS';
plot(data.Var2,data.Var3,'b--','LineWidth',1)
hold on
plot(data.Var2,data.Var4,'b--','LineWidth',1)
plot(data.Var2,data.Var5,'g--','LineWidth',1)
plot(data.Var2,data.Var6,'g--','LineWidth',1)
hold off
Márcio Gonçalves
2020년 4월 13일
I attached my csv file. The time is in colum 2.
thanks
Cris LaPierre
2020년 4월 13일
The best way to figure out how to read in the data is the Import Tool. It allows you to interactively set the data types, variable names, and visually inspect the results. Once you have it working, you can generate an import script or function for reuse.
With your data file, I went through and manually created the code to import the data and then format the data. Here's what that ended up looking like.
% Define the import options
opts = detectImportOptions('Marcio_mytest1.csv');
opts.DataLines = [2,inf];
opts.Delimiter = ";";
opts.VariableNames = {'Sweep','Time','Ch101','Ch102','Ch103','Ch104','Ch105',...
'Ch106','Ch107','Ch108','Ch109','Ch110','Ch111','Ch112','Ch113','Ch114','Ch115',...
'Ch116','Ch117','Ch118','Ch119','Ch120','Ch201','Ch202','Ch203','Ch204','Ch205',...
'Ch206','Ch207','Ch208','Ch209','Ch210','Ch211','Ch212','Ch213','Ch214','Ch215',...
'Ch216','Ch217','Ch218','Ch219','Ch220','Ch301','Ch302','Ch303','Ch304','Ch305',...
'Ch306','Ch307','Ch308','Ch309','Ch310','Ch311','Ch312','Ch313','Ch314','Ch315',...
'Ch316','Ch317','Ch318','Ch319','Ch320'};
opts = setvartype(opts,[1,3:62],"double");
opts = setvartype(opts,'Time',"datetime");
opts = setvaropts(opts, 'Time', "InputFormat", "MM/dd/yyyy HH:mm:ss.SSS");
% Read in the data file
data = readtable('test1.csv',opts);
% Set display date format
data.Time.Format = 'dd-MM-yyyy HH:mm:ss.SS';
% Plot
plot(data.Time,data.Ch101,'b--','LineWidth',1)
hold on
plot(data.Time,data.Ch102,'b--','LineWidth',1)
plot(data.Time,data.Ch103,'g--','LineWidth',1)
plot(data.Time,data.Ch104,'g--','LineWidth',1)
hold off
Cris LaPierre
2020년 4월 13일
편집: Cris LaPierre
2020년 4월 27일
If you want to specifically set the display format of the data on the axes, use the datetick function.
Peter Perkins
2020년 4월 14일
Don't use the datetick function unless you have a veru old version of MATLAB. Stay away from datenum, datestr, and datetick. In fact, no offense to earlier responders intended, don't do any of that.
In a recent version of MATLAB, here's all you need:
>> tt = readtimetable('test1.csv','RowTimes','Time','PreserveVariableNames',true);
>> head(tt)
ans =
8×61 timetable
Time Sweep # Chan 101 (C) Chan 102 (C) Chan 103 (C) Chan 104 (C) Chan 105 (C) Chan 106 (C) Chan 107 (C) Chan 108 (C) Chan 109 (C) Chan 110 (C) Chan 111 (C) Chan 112 (C) Chan 113 (C) Chan 114 (C) Chan 115 (C) Chan 116 (C) Chan 117 (C) Chan 118 (C) Chan 119 (C) Chan 120 (C) Chan 201 (C) Chan 202 (C) Chan 203 (C) Chan 204 (C) Chan 205 (C) Chan 206 (C) Chan 207 (C) Chan 208 (C) Chan 209 (C) Chan 210 (C) Chan 211 (C) Chan 212 (C) Chan 213 (C) Chan 214 (C) Chan 215 (C) Chan 216 (C) Chan 217 (C) Chan 218 (C) Chan 219 (C) Chan 220 (C) Chan 301 (VDC) Chan 302 (VDC) Chan 303 (VDC) Chan 304 (VDC) Chan 305 (VDC) Chan 306 (VDC) Chan 307 (VDC) Chan 308 (VDC) Chan 309 (VDC) Chan 310 (VDC) Chan 311 (VDC) Chan 312 (VDC) Chan 313 (VDC) Chan 314 (VDC) Chan 315 (VDC) Chan 316 (VDC) Chan 317 (VDC) Chan 318 (VDC) Chan 319 (C) Chan 320 (C)
_______________________ _______ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ____________ ____________
03/27/2020 19:10:35.537 1 15.4 15.4 15.3 15.2 16 15.5 15.4 15.3 15.3 15.3 20.3 20.8 20.5 21 20.1 20.2 20.2 20.3 19.1 19 18.3 19.3 18.2 19.5 17.8 18.6 23 15.8 15.7 20.5 21.1 20.8 20.4 22.6 20.5 20.5 19.7 21.1 19.6 16.2 20.5 73.1 -24.3 99.6 20 74.9 19.9 82.7 20 81.3 15.9 100 4.34e-05 4.07e-05 -3.79e-05 -4e-05 -3.97e-05 -5.59e-05 18 18.6
03/27/2020 19:15:35.536 2 15.4 15.4 15.3 15.2 16 15.5 15.4 15.3 15.4 15.3 21 21.4 21.1 21.8 20.8 20.9 20.9 20.9 19.7 19.6 18.8 20 18.7 20.2 18.3 19.2 23.3 15.8 15.7 21.1 21.7 21.5 21 23.2 21 21.1 20.4 21.7 20.2 16.1 21.2 72.8 -24.3 99.6 20.7 74.5 20.4 82.3 20.6 80.8 15.9 100 4.22e-05 3.76e-05 -3.97e-05 -3.71e-05 -4.02e-05 -5.49e-05 18.3 19
03/27/2020 19:20:35.536 3 15.4 15.4 15.3 15.2 16 15.5 15.4 15.3 15.4 15.3 21.4 21.9 21.6 22.3 21.3 21.3 21.3 21.4 20.2 20.2 19.2 20.5 19.1 20.7 18.7 19.8 23.4 15.8 15.7 21.6 22.2 21.9 21.5 22.4 21.5 21.6 22.5 22.1 20.7 16.2 21.7 72.5 -24.3 99.6 21.2 74.4 21 81.9 21.2 80.3 15.9 100 4.25e-05 3.23e-05 -3.83e-05 -3.29e-05 -3.88e-05 -5.12e-05 18.6 19.2
03/27/2020 19:25:35.536 4 15.4 15.4 15.4 15.2 16 15.5 15.4 15.3 15.4 15.3 21.7 22.1 21.9 22.4 21.6 21.6 21.6 21.8 20.6 20.6 19.5 21 19.4 21.1 19 20.1 23.4 15.8 15.7 21.9 22.4 22.1 21.8 22.4 21.8 22 22.7 22.3 21 16.1 22 72.3 -24.2 99.6 21.5 74 21.4 81.5 21.5 79.9 15.9 100 3.88e-05 2.65e-05 -3.73e-05 -2.78e-05 -3.72e-05 -4.77e-05 18.9 19.3
03/27/2020 19:30:35.576 5 15.4 15.5 15.4 15.2 16 15.5 15.5 15.3 15.5 15.4 21.9 22.3 22 22.5 21.9 21.8 21.8 22 20.9 20.9 19.7 21.3 19.6 21.3 19.2 20.4 23.3 15.8 15.7 22.1 22.6 22.3 22.1 22.5 22 22.2 22.7 22.5 21.3 16.2 22.3 72.1 -24.1 99.5 21.8 73.9 21.6 81.2 21.8 79.7 15.9 100 3.77e-05 2.31e-05 -3.55e-05 -2.33e-05 -3.66e-05 -4.46e-05 19.1 19.5
03/27/2020 19:35:35.574 6 15.5 15.5 15.4 15.2 16 15.6 15.5 15.3 15.5 15.4 22.1 22.5 22.1 22.6 22.1 22 21.9 22.2 21.1 21.1 19.9 21.5 19.7 21.5 19.4 20.5 23.5 15.8 15.7 22.3 22.7 22.4 22.2 22.4 22.2 22.3 22.8 22.5 21.5 16.2 22.5 72 -24.1 99.5 22 73.8 21.8 81 21.9 79.5 16 100 3.53e-05 1.93e-05 -3.59e-05 -2.12e-05 -3.64e-05 -4.28e-05 19.2 19.6
03/27/2020 19:40:35.536 7 15.5 15.5 15.4 15.2 15.5 15.6 15.5 15.3 15.5 15.4 22.2 22.6 22.2 22.6 22.2 22 22 22.3 21.2 21.2 20 21.7 19.8 21.6 19.5 20.7 23.3 15.8 15.7 22.4 22.8 22.5 22.4 22.4 22.3 22.5 22.8 22.6 21.7 16.1 22.7 72 -24.1 99.4 22.2 73.7 21.9 80.7 22.1 79.3 16 100 3.47e-05 1.67e-05 -3.5e-05 -1.91e-05 -3.51e-05 -4.15e-05 19.2 19.6
03/27/2020 19:45:35.546 8 15.5 15.5 15.5 15.2 15.5 15.6 15.5 15.4 15.5 15.5 22.2 22.6 22.2 22.7 22.3 22.2 22.2 22.4 21.3 21.4 20.1 21.8 19.9 21.8 19.6 20.8 23.4 15.8 15.7 22.5 22.9 22.6 22.5 22.5 22.4 22.6 22.9 22.7 21.8 16.1 22.7 71.9 -24.1 99.5 22.3 73.6 22.1 80.6 22.2 79.2 16 100 3.52e-05 1.66e-05 -3.57e-05 -1.85e-05 -3.65e-05 -4.13e-05 19.3 19.6
>> stackedplot(tt(:,1:10))

In less recent versions, you won't be able to use PreserveVariableNames, they will be modified with a warning. And you won't be able to use readtimetiable, use readtable and table2timetable.
If stackedplot isn't doing what you need, you can make whatever other plot you want with something like
plot(tt.Time,tt.Chan101_C_)
but don't use datetick on a datetime plot. For sure, don't do this:
D = data{:,2};
d = datenum(D);
date = datetime(d,'Format','dd-MM-yyyy HH:mm:ss.SS','convertFrom','datenum');
If the data are read in as text, convert them directly to datetime. In recent versions, read them in directly as datetimes using detectimportoptions.
Márcio Gonçalves
2020년 4월 14일
Thank you all for your answers.
I will try your suggestions as soon as possible and I give you the feedback.
My matlab version is R2019a.
추가 답변 (1개)
Márcio Gonçalves
2020년 4월 20일
0 개 추천
Dear Cris LaPierre,
Your code works perfectly. Thank you.
Dear Peter Perkins,
My matlab doesn't work with "PreserveVariableNames".
Thanks
댓글 수: 2
Peter Perkins
2020년 4월 27일
편집: Peter Perkins
2020년 4월 27일
As I said, "In less recent versions, you won't be able to use PreserveVariableNames, they will be modified with a warning. And you won't be able to use readtimetiable, use readtable and table2timetable." Unless you are using a really old version of MATLAB, I really, strongly, recommend that you try what I have suggested.
Márcio Gonçalves
2020년 4월 27일
ok, thank you for your help.
카테고리
도움말 센터 및 File Exchange에서 Calendar에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
