error in using year()

1 回表示 (過去 30 日間)
Binzi Shu
Binzi Shu 2016 年 2 月 29 日
編集済み: Stephen23 2016 年 3 月 1 日
I typed exactly the same code as in the reference provided below:
Year = year('05-Aug-2003')
and I get
Year =
2002
2002
2002
2002
2002
2002
2002
2002
2002
2002
2002
Why is that? Thanks.
Reference:
  2 件のコメント
Fangjun Jiang
Fangjun Jiang 2016 年 2 月 29 日
what is the version of the Financial Toolbox?
ver('finance')
which -all year
Binzi Shu
Binzi Shu 2016 年 3 月 1 日
v5.6 for financial toolbox

サインインしてコメントする。

回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2016 年 3 月 1 日
編集済み: Fangjun Jiang 2016 年 3 月 1 日
There must be a variable called "year" in the workspace. Try this:
year=2002*ones(120,1);
Year = year('05-Aug-2003')
It got your answer.
Try this
year=1:120;
Year=year('05-Aug-2003')
Year =
48 53 45 65 117 103 45 50 48 48 51
notice
>> double('05-Aug-2003')
ans =
48 53 45 65 117 103 45 50 48 48 51
It turned out it converted the string '05-Aug-2003' into double numbers and used the numbers to index the existing matrix "year". The size of "year" happens to be large enough so the "Index exceeds matrix dimensions" error didn't happen.
I don't like this behavior.
  2 件のコメント
Binzi Shu
Binzi Shu 2016 年 3 月 1 日
yes there is already a variable called year and it is a 1*n matrix. Does that mean that the year() function will not work properly if I ever created a variable called year? Thanks.
Stephen23
Stephen23 2016 年 3 月 1 日
編集済み: Stephen23 2016 年 3 月 1 日
@Binzi Shu: that is correct. When you create a variable with the same name as an inbuilt function/variable, you cannot use the inbuilt one. For this reason avoid using length, size, cell, i, j, year, etc.
You can check if a name is already used with the which function.

サインインしてコメントする。

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by