Plotting a time series

6 ビュー (過去 30 日間)
VS
VS 2023 年 9 月 6 日
回答済み: Seth Furman 2023 年 9 月 13 日
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

回答 (3 件)

VINAYAK LUHA
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,

Christian Niklaus
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});

Seth Furman
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)
tt = 607×3 timetable
Date Loans Deposits Loans to Deposit Ratio _______ __________ __________ ______________________ 1973-01 5.6664e+05 5.9689e+05 0.94933 1973-02 5.7152e+05 5.9896e+05 0.9542 1973-03 5.8174e+05 6.0299e+05 0.96476 1973-04 5.8885e+05 6.124e+05 0.96155 1973-05 5.9511e+05 6.1435e+05 0.96868 1973-06 6.0397e+05 6.2306e+05 0.96936 1973-07 6.1121e+05 6.3079e+05 0.96895 1973-08 6.1738e+05 6.2993e+05 0.98007 1973-09 6.2312e+05 6.3863e+05 0.97572 1973-10 6.2697e+05 6.4764e+05 0.96809 1973-11 6.3237e+05 6.492e+05 0.97407 1973-12 6.43e+05 6.6382e+05 0.96863 1974-01 6.4435e+05 6.6474e+05 0.96932 1974-02 6.4524e+05 6.6362e+05 0.97231 1974-03 6.56e+05 6.6776e+05 0.98239 1974-04 6.6941e+05 6.8458e+05 0.97784
stackedplot(tt)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by