Undefined operator './' for input arguments of type 'handle.handle'

15 ビュー (過去 30 日間)
Peter_SG
Peter_SG 2024 年 10 月 21 日
コメント済み: Umar 2024 年 10 月 24 日 1:12
Hi,
I have the following code to calculate daily returns:
dates = datenum(AIG.DatesAtStartOfPeriod, 'dd/mm/yyyy');
formatOut = 'mmm-yyyy';
DateString = datestr(dates,formatOut);
T = [AIG, GE, IBM];
symbol = {'AIG','GE','IBM'};
nAsset = numel(symbol);
When I try calculating these returns with:
ret = T(2:end,:)./T(1:end-1,:)-1
I get the error:
"Undefined operator './' for input arguments of type 'handle.handle'"
When I replace this with the following function:
ret = tick2ret(T{:,symbol});
I get the following error:
"Brace indexing is not supported for variables of this type"
I would appreciate your assistance on what I may be overlooking - many thanks.
  2 件のコメント
Matt J
Matt J 2024 年 10 月 21 日
Your posted code doesn't reveal anything about what T is. Best would be to attach it in a .mat file so we can examine it for ourselves.
Image Analyst
Image Analyst 2024 年 10 月 21 日
編集済み: Image Analyst 2024 年 10 月 21 日
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
So, before the error do
save('answers.mat', 'T', 'AIG', 'GE', 'IBM');
and attach the answers.mat file with the paperclip icon in your original question.
By the way AIG is a structure (or a class) so if
T = [AIG, GE, IBM];
worked then T, GE, and IBM must also be structures or classes of the same type. And you can't divide a structure by a structure. There are many elements to a structure and it can't just divide all matching field names - it wouldn't make sense in the general case.

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

採用された回答

Umar
Umar 2024 年 10 月 22 日

Hi @Peter_SG ,

To calculate daily returns in MATLAB, it is essential to ensure that the data structure you are working with is compatible with the operations you intend to perform. The errors you are encountering suggest that the data type of T is not suitable for the operations you are trying to execute. Below, I will provide a detailed explanation of the issues and a complete, updated code snippet to resolve them.

Undefined operator ./ for input arguments of type handle.handle: This error indicates that T is not a standard numeric array or table but rather a type that does not support element-wise division. This often occurs when using a table or a custom object that does not implement the required operations.

Brace indexing is not supported for variables of this type: This error arises when you attempt to use curly braces { } to index into a variable that does not support this type of indexing. In MATLAB, curly braces are typically used for cell arrays or tables, and if T is not structured correctly, this will lead to an error.

To resolve these issues, I will ensure that T is a numeric array containing the closing prices of the assets. Here’s how you can structure your code:

% Sample Data Creation (Replace this with your actual data)
AIG.DatesAtStartOfPeriod = {'01/01/2023'; '02/01/2023'; '03/01/2023'};
AIG.Prices = [100; 102; 101]; % Example prices for AIG
GE.Prices = [50; 51; 52]; % Example prices for GE
IBM.Prices = [150; 148; 149]; % Example prices for IBM
% Convert dates to MATLAB date numbers
dates = datenum(AIG.DatesAtStartOfPeriod, 'dd/mm/yyyy');
formatOut = 'mmm-yyyy';
DateString = datestr(dates, formatOut);
% Create a table with the prices
T = table(AIG.Prices, GE.Prices, IBM.Prices, 'VariableNames', {'AIG', 'GE', 
'IBM'});
% Calculate daily returns
% Ensure T is a numeric array for calculations
priceData = table2array(T); % Convert table to array
ret = priceData(2:end, :) ./ priceData(1:end-1, :) - 1; % Calculate returns
% Display the results
disp('Daily Returns:');
disp(ret);

Please see attached.

This updated code should resolve the errors you encountered and allow you to calculate daily returns effectively. Ensure that your actual data is structured similarly to the sample data provided.

Please bear in mind that @Image Analyst and @Matt J have years of experience and their opinions matter. They have helped tackle lot of complex problems.

Hope this helps.

If you have any further questions or need additional assistance, feel free to ask!

  3 件のコメント
Peter_SG
Peter_SG 2024 年 10 月 23 日 14:39
移動済み: Voss 2024 年 10 月 23 日 15:14
Thank you @Umar for your extensive explanation, and also to @Matt J for your earlier reply. They're much appreciated. I understand the issue now and I am now able to run the code.
Umar
Umar 2024 年 10 月 24 日 1:12
Hi @ Peter_SG,
Thank you for your kind words. I am glad to hear that my explanation, along with Matt's earlier response, has clarified the issue for you. It is great to know that you are now able to run the code successfully.

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by