Function Error: Already declared
이전 댓글 표시
My function was working fine until I typed in clear all. Now it does not work.
I get this error message: Function 'get_portfolio_return' has already been declared within this scope.
This is my function code:
function [E_Rp, sigma_Rp] = get_portfolio_return( weight,exp_rets, vols,corr)
E_Rp= weight.* exp_rets(1,1)+ (1-weight).*exp_rets(1,2);
sigma_Rp= sqrt((weight.^2.*vols(1).^2) + ((1-weight).^2.*vols(2).^2)+(2.*corr.*(weight)).*(1-weight).*vols(1).*vols(2));
end
% The stated function defines the necessary variables for subsequent
% calculations. Expected Return is calculated as a function of weight and
% expected returns. Weight is the weight of Asset1 and exp_rets are the
% expected returns of asset 1 and asset 2.
What caused this problem/ how do I fix it?
댓글 수: 1
Jeremy
2019년 11월 11일
Did you save this as a function file separate from your main script file, or did you put it at the bottom of your script as a local function? Did you do both? How are you calling it?
채택된 답변
추가 답변 (2개)
Muhammad Sinan
2019년 12월 30일
1 개 추천
When define user function in MATLAB programming then you will need the following format.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function (Dependent Variables1, 2, 3, ...) = FunctionName(Independent Variables1, 2, 3, ...)
body of function
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
so, when you save this file then you have to use this file on the name same as the name of the function. Otherwise, the function will not work.
Example:
FunctionName.m
댓글 수: 2
Sian Evans
2020년 6월 5일
My first function name and the file name are the same yet I still get this error? I don't understand how to bypass it?
Walter Roberson
2020년 6월 5일
What is the first executable word in the file? The very first thing that is not a comment?
Sumanth Hegde
2021년 12월 11일
0 개 추천
I got this error because I declared the same function name twice.
카테고리
도움말 센터 및 File Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!