Plotting a time series
6 views (last 30 days)
Show older comments
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 Comments
Answers (3)
VINAYAK LUHA
on 6 Sep 2023
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 Comments
Christian Niklaus
on 6 Sep 2023
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 Comments
Seth Furman
on 13 Sep 2023
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 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!