Undefined function or variable 'Date2Num'. I am not sure whether this is due to a mix in coding from Python where Date2Num is used to convert date time objects to Matplotlib dates. What is the corresponding function in Matlab? Thanks in advance.
Error in VARToolbox_Primer (line 15)
datesnum = Date2Num(dates);

댓글 수: 7

Walter Roberson
Walter Roberson 2021년 1월 24일
MATLAB does not have native Matplotlib dates .
What is the input data type? What is the required output data type? Are you asking to convert datetime() objects to serial date numbers? If so, then datenum() applied to the datetime() objects.
The coding says the following:
% Load data from US macro data set
[xlsdata, xlstext] = xlsread('data/MACRO_US.xlsx','Sheet1');
dates = xlstext(3:end,1);
datesnum = Date2Num(dates);
vnames_long = xlstext(1,2:end);
vnames = xlstext(2,2:end);
nvar = length(vnames);
data = Num2NaN(xlsdata);
and sheet1 refers to quarterly data for CPI, GDP, Unemployment etc time series.
Walter Roberson
Walter Roberson 2021년 1월 24일
What is done with datesnum ?
It looks to me as if you should likely use readtable() instead of xlsread()
economics student
economics student 2021년 1월 24일
readtable () does not seem to work but xlsread () does. Then again there is the issue with
datesnum = Date2Num(dates)
Walter Roberson
Walter Roberson 2021년 1월 24일
What error message do you get with readtable()?
economics student
economics student 2021년 1월 24일
Undefined function or variable 'Date2Num'.
Error in VARToolbox_Primer (line 15)
datesnum = Date2Num(dates);
attached is the excel sheet. data come from sheet 1. As can be seen the dates are titled with t. But even when i try DateNumber = datenum(t) it doesnt work either.
dpb
dpb 2021년 1월 24일
Date2Num is not builtin MATLAB function so looks like you're missing an m-file that went with the code you do have or if it is an attempt to use a Python function I believe you have to import it somehow--I've never done that so don't know the specifics.

댓글을 달려면 로그인하십시오.

 채택된 답변

dpb
dpb 2021년 1월 24일
편집: dpb 2021년 1월 24일

1 개 추천

As noted above I don't know for certain whether the Date2Num is just a missing m-file or an attempt to use a Python function.
You don't need it though, and converting to use the new(ish) table object is probably worth the effort. It takes only a little bit of help for readtable and you have all the data in one table instead of text and numerics separate as with the deprecated xlsread route.
optMACRO=detectImportOptions('MACRO_US.xlsx','Sheet','Sheet1','NumHeaderLines',1);
tMACRO=readtable('MACRO_US.xlsx',optMACRO);
tMACRO.t=datetime(tMACRO.t,'InputFormat','yyyyQ','Format','yyyy''q''Q');
the result for the attached file is
>> head(tMACRO)
ans =
8×7 table
t cpi gdp unemp vix i1yr ebp
______ ______ _____ ______ _____ ____ _________
1989q1 51.333 52.3 5.2 17.82 9.29 0.4049
1989q2 52.162 52.7 5.2333 17.02 8.93 0.5758
1989q3 52.57 53.09 5.2333 18.08 8.1 0.7444
1989q4 53.105 53.19 5.3667 20.35 7.83 0.5925
1990q1 54.019 53.77 5.3 21.75 8.13 0.38887
1990q2 54.553 53.97 5.3333 18.68 8.27 0.14737
1990q3 55.495 54 5.7 25.08 7.83 -0.031533
1990q4 56.438 53.51 6.1333 25.99 7.3 0.2838
>>

댓글 수: 10

economics student
economics student 2021년 1월 24일
Thanks so much. The instructions mention the following:
To save figures in high quality format, you need to download an install Ghostscript (freely available at www.ghostscript.com).
To avoid clashes with functions from other toolboxes, it is recommendable to add and remove the Toolbox at beginning and end of your scripts I If you download the toolbox to /User/VAR-Toolbox/, you can simply add the following lines at the beginning and end of your script addpath(genpath(’/User/VAR-Toolbox/v3dot0/’)) ... rmpath(genpath(’/User/VAR-Toolbox/v3dot0’)) .
so yes, maybe this is the issue. Shall try both options.
dpb
dpb 2021년 1월 24일
편집: dpb 2021년 1월 24일
That's certainly klunky set of instructions...
There's a whole plethora of other folders of code at that site -- Date2Num is in the folder FIGURE
You'll probably need all that code to make the package work.
The file you downloaded it appears is just a script that shows 10-12 uses of the content of the package all strung together in one huge script; once you get past that part you'll run into missing all the other pieces, too.
I didn't see how one is supposed to download/install the whole thing as a toolbox but you'll have to do that to get anywhere.
The output of Date2Num is a replacement for datenum with some particular ways it handles econometrics data it appears. You'll probably end up needing to revert to it to use their plotting routines; the new MATLAB plot functions are datetime aware, but to use datenums or similar will take still futzing with the old dateticks function to put date information on an axis.
economics student
economics student 2021년 1월 24일
편집: dpb 2021년 1월 24일
It is from 2012 but updated in November 2020, don´t know to what extent.
Matlab seems to read well the new commands, however this comes by since it was previously specified in the loading of the data:
% Store variables in the structure DATA
for ii=1:length(vnames)
DATA.(vnames{ii}) = data(:,ii);
end
Undefined function or variable 'vnames'.
Error in VARToolbox_Primer (line 17)
for ii=1:length(vnames)
economics student
economics student 2021년 1월 24일
Thanks in any case.
dpb
dpb 2021년 1월 24일
편집: dpb 2021년 1월 24일
At <VAR-Toolbox/tree/main/v3dot0> are some 8-9 folders of code; you'll need all the content of all of those to run the single VARToolbox_Primer.m script you downloaded; it calls stuff scattered around in all of those.
[xlsdata, xlstext] = xlsread('data/MACRO_US.xlsx','Sheet1');
dates = xlstext(3:end,1);
datesnum = Date2Num(dates);
vnames_long = xlstext(1,2:end);
vnames = xlstext(2,2:end);
nvar = length(vnames);
data = Num2NaN(xlsdata);
...
is where vnames array is defined; it is dependent upon the form of the data having been loaded using xlsread where the text data were/are not imported with the numerical nor read as variable names for the table as done above with readtable.
It will take getting all the ancillary routines needed (you haven't gotten to any of the analyses, yet, this is still just input processing) to make this run.
It would seem there should be a high-level link for installation of the whole package but I didn't see how that was set up and I haven't used GitHub enough to know; what I kinda' remember is there is a GitHub app that is used to manage stuff.
Anyway, all your troubles are coming from not having fully installed the whole toolbox; go figure out how to do that and then all will probably work as advertised.
economics student
economics student 2021년 1월 26일
Many thanks! I have contacted the author but no reply for the time being. The issue is that all folders can be seen in Matlab aswell as the excel sheets in the workspace. Other users do not seem to have any concerns as far as the evaluations in Github showcase . I have run similar programmes with interlinked folders and usually the examples run correctly.
So what can be the matter? Would you please mind trying to run the programme?
dpb
dpb 2021년 1월 26일
편집: dpb 2021년 1월 26일
In MATLAB, everything has to be on MATLAB SEARCH PATH to be visible or in a private subdirectory under the upper level. See <What-is-the-matlab-search-path> for the full skinny of how MATLAB resolves file and/or function names.
The long and short of it is that if
which -all wantedfunctionname
doesn't return the path in which the wantedfunctionname and all its aliases reside but the 'Not found' message, then either the subject function m-file is not on the system at all or if it is, the location where it resides has not been added to the path.
economics student
economics student 2021년 1월 27일
Thanks so much!!
On the Home tab, in the Environment section, click Set Path. The Set Path dialog box appears. Use the Set Path dialog box to modify the search path. Added the programme with subfolders.
Walter Roberson
Walter Roberson 2021년 1월 27일
I recommend using pathtool as it gives a nice visual interface.
With pathtool items that are higher up in the list have priority over items lower down in the list.
economics student
economics student 2021년 1월 30일
Thanks very much.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

질문:

2021년 1월 24일

댓글:

2021년 1월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by