Call Function from .m file in command

147 visualizaciones (últimos 30 días)
L McKeehan
L McKeehan el 12 de Sept. de 2021
Comentada: L McKeehan el 16 de Sept. de 2021
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

Respuesta aceptada

Dave B
Dave B el 13 de Sept. de 2021
@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 comentarios
Dave B
Dave B el 14 de Sept. de 2021
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 el 16 de Sept. de 2021
Thank you so much! now i totally get it!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by