Call Function from .m file in command

조회 수: 141 (최근 30일)
L McKeehan
L McKeehan 2021년 9월 12일
댓글: L McKeehan 2021년 9월 16일
Hi, So I have the below function that is saved in a .m file. All I want to do is call this function fromt he command line and enter in the values, but it keeps saying "Unrecognized function or variable 'CalculateLosses'."
function [Total_losses]= CalculateLosses(Output,Ron1,Ron2,Rg1,Rg2,Qg1,Qg2,Qrr1,Qrr2,Vth1,Vth2,Qo1,Qo2)
hs = lm25119_calcs;
hs.RdsOn = Ron1;
hs.Rg = Rg1;
hs.Qg = Qg1;
hs.Qrr = Qrr1;
hs.Vth = Vth1;
hs.Qoss = Qo1;
hs.Output = Output;
ls = lm25119_calcs;
ls.RdsOn = Ron2;
ls.Rg = Rg2;
ls.Qg = Qg2;
ls.Qrr = Qrr2;
ls.Vth = Vth2;
ls.Qoss = Qo2;
ls.Output = Output;
HsLoss = hs.HsFETlosses(hs,Output);
LsLoss = ls.LsFETlosses(ls,Output);
Total_losses = LsLoss +HsLoss;
end

채택된 답변

Dave B
Dave B 2021년 9월 13일
@L McKeehan - perhaps the most common reason for this is that the function is not on MATLAB's search path. When you try to run a function, MATLAB looks in the current directory and in a list of directories that MATLAB seaches for functions.
To change the directory, you can use cd or you can find the little browse button near the top of the MATLAB window:
To see the current directory, you can look just to the right of that brows button or use pwd
It's often useful to not change the directory to run a script and instead keep your scripts in a folder on the path. This way, if you have two different scripts in two different folders, you don't have to worry about keeping track. There's a nice interactive menu for adding directories to the path, and you can get there by running the function pathtool
A final reason that your function might not be found is if the name of the file doesn't match the name of the function. Make sure that they are named the same thing.
To simplify a lot of this, if you open the function in the MATLAB editor (you can just drag it into the editor window) and click the Run button, MATLAB will prompt you (if it's not on the path) to either change the directory or add to the path.
  댓글 수: 3
Dave B
Dave B 2021년 9월 14일
if you have an m file with multiple functions, then the first one (which is the same name as the file) is accessible from other functions and the base workspace, but the remaining functions, called subfunctions, are only accessible locally.
There are many workarounds, one that I like is to use a class with several static functions, the class then works as a namespace for bunch of related functions:
In an m file called calculator.m:
classdef (Abstract) calculator %the Abstract is not essential, but will prevent X = calculator;
methods(Static)
function c=add(a,b)
c=a+b;
end
function c=multiply(a,b)
c=a*b;
end
end
end
At the command line:
>> a=calculator.add(1,2)
a =
3
>> a=calculator.multiply(1,2)
a =
2
L McKeehan
L McKeehan 2021년 9월 16일
Thank you so much! now i totally get it!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by