Undefined operator './' for input arguments of type 'handle.handle'
2 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!