Datetime errors when I import data
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
William Milton
el 4 de Ag. de 2022
Comentada: Cris LaPierre
el 4 de Ag. de 2022
Hi, I'm a beginner in Matlab. I found out Matlab allows me to apply the Nelson-Siegel model easily for my thesis. Hence, I replicated this code https://it.mathworks.com/help/fininst/fitnelsonsiegelirfunctioncurve.html with my own data. As far as here it's all right. However it's time wasting to insert bond data manually so I tried to use the same code, but importing a dataset. This is the code:
%% Import data from spreadsheet
% Script for importing data from the following spreadsheet:
%
% Workbook: C:\Users\William\Desktop\TestMatlab.xlsx
% Worksheet: Foglio1
%
% Auto-generated by MATLAB on 04-Aug-2022 10:35:06
%% Set up the Import Options and import the data
opts = spreadsheetImportOptions("NumVariables", 3);
% Specify sheet and range
opts.Sheet = "Foglio1";
opts.DataRange = "A2:C21";
% Specify column names and types
opts.VariableNames = ["Scadenza", "PrezzoMercato", "Cedola"];
opts.VariableTypes = ["datetime", "double", "double"];
% Specify variable properties
opts = setvaropts(opts, "Scadenza", "InputFormat", "");
% Import the data
TestMatlab = readtable("C:\Users\William\Desktop\TestMatlab.xlsx", opts, "UseExcel", false);
%% Clear temporary variables
clear opts
Settle = repmat(datetime('12-Sep-2001'),[20 1]);
Maturity = datetime('Scadenza');
CleanPrice = ('PrezzoMercato');
CouponRate = ('Cedola');
Instruments = [Settle Maturity CleanPrice CouponRate];
PlottingPoints = datetime('15-Feb-2002'):180:datetime('01-Aug-2011');
Yield = bndyield(CleanPrice,CouponRate,Settle,Maturity);
NSModel = IRFunctionCurve.fitNelsonSiegel('Zero',datenum('12-Set-2001'),Instruments);
NSModel.Parameters
plot(PlottingPoints, getParYields(NSModel, PlottingPoints),'r','Color',[0 0 0])
hold on
scatter(Maturity,Yield,'black')
datetick('x')
So I get an datetime error that says:
"Error using datetime
Could not recognize the date/time format of 'Scadenza'. You can specify a format using the
'InputFormat' parameter. If the date/time text contains day, month, or time zone names in a language
foreign to the 'en_US' locale, those might not be recognized. You can specify a different locale using
the 'Locale' parameter".
I tried so follow the procedure but I get other errors.
Any advice?
2 comentarios
dpb
el 4 de Ag. de 2022
Without the format of the data you're trying to read, not much we can do...attach a sample input file or at least give an example string to try to convert.
Respuesta aceptada
Cris LaPierre
el 4 de Ag. de 2022
Editada: Cris LaPierre
el 4 de Ag. de 2022
You are passing a character array into the datetime function, not a variable
Maturity = datetime('Scadenza');
This line of code is unnecessary, as you've already imported this table variable as datetime. It looks like instead this page on accessing data in tables is what you need.
So your code should be
Maturity = TestMatlab.Scadenza;
2 comentarios
Cris LaPierre
el 4 de Ag. de 2022
Here's how I would do it.
fileNm = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1088350/TestMatlab.xlsx';
tbl = readtable(fileNm)
Maturity = datenum(tbl.Scadenza);
CleanPrice = tbl.PrezzoMercato;
CouponRate = tbl.Cedola;
Settle = repmat(datenum('12-Sep-2001'),[20 1]);
Instruments = [Settle Maturity CleanPrice CouponRate];
PlottingPoints = Maturity(1):180:Maturity(20);
Yield = bndyield(CleanPrice,CouponRate,Settle,Maturity);
NSModel = IRFunctionCurve.fitNelsonSiegel('Zero',Settle(1),Instruments);
NSModel.Parameters
plot(PlottingPoints, getParYields(NSModel, PlottingPoints),'r','Color',[0 0 0])
hold on
scatter(Maturity,Yield,'black')
hold off
datetick('x')
Más respuestas (1)
Steven Lord
el 4 de Ag. de 2022
When you selected options in the Import Tool, you told it that you wanted it to import your Scadenza data as a datetime array. But you didn't tell the tool the format in which your data had been stored, based on this line in the generated code.
opts = setvaropts(opts, "Scadenza", "InputFormat", "");
If you go back to the Import Tool and specify the format in which that data is stored in your file, that may avoid the error you received. See the "Specify Decimal Separator and Date Format" example on this documentation page for an example of how to specify that format. You can choose one of a set of predefined options or you can specify your own using the identifiers in the tables in the description of the Format property for datetime objects. It's the first entry in the Properties section on that documentation page.
This assumes that the time and date data stored in your file is in a consistent format through the whole file. If you switch data formats halfway through the file, that's more challenging.
Ver también
Categorías
Más información sobre Spreadsheets en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!