Plotting a time series
조회 수: 6 (최근 30일)
이전 댓글 표시
I am trying to plot a time series in which date appears on the x-axis and the variable of interest appears on y-axis. My data contains dates in the format of 1973-01, as in YYYY-MM. I am trying to import to import the data and plot but it keeps throwing the following error message
>> loantodepositratio
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for
the table. The original column headers are saved in the VariableDescriptions property.
Set 'PreserveVariableNames' to true to use the original column headers as table variable names.
Error using plot
Invalid data argument.
Error in loantodepositratio (line 2)
plot(info{:,1},info{:,4});
Will apprciate any help. I attach the data file and here is the code I am trying to use for the plot:
info = readtable('data3.xls');
plot(info{:,1},info{:,4});
recessionplot;
print -dpdf loantodepositratio
댓글 수: 0
답변 (3개)
VINAYAK LUHA
2023년 9월 6일
Hi VS,
Typecast the info.Date to MATLAB datetime format before plotting like below.
info = readtable('data3.xls',VariableNamingRule='preserve');
plot(datetime(info.Date,"Format",'yyy-mm'),info.("Loans to Deposit Ratio"))
Hope this helps,
댓글 수: 0
Christian Niklaus
2023년 9월 6일
You can do it by changing your first two lines into:
info = readtable('data3.xls','VariableNamingRule','preserve');
info.Date = datetime(info.Date,'InputFormat','yyyy-MM');
plot(info{:,1},info{:,4});
Short description:
With the additional argument 'VariableNamingRule','preserve' in the function readtable you define that the first row describes the column names. So, you will get no warning any more.
Your column "Date" will be imported per default as text. Therefore, your have to convert it to a datetime column. You can do this with the function datetime together with the input format (here: 'yyyy-MM' for year and month). Afterwards, you can plot the imported data as you have done it with plot(info{:,1},info{:,4});
댓글 수: 0
Seth Furman
2023년 9월 13일
fname = "data3.xls";
opts = detectImportOptions(fname,TextType="string",VariableNamingRule="preserve");
opts = setvaropts(opts,"Date",Type="datetime",InputFormat="uuuu-MM");
tt = readtimetable(fname,opts)
stackedplot(tt)
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!