MATLAB Answers

Call Function from .m file in command

7 views (last 30 days)
L McKeehan
L McKeehan on 12 Sep 2021
Commented: L McKeehan on 16 Sep 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

Accepted Answer

Dave B
Dave B on 13 Sep 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 Comments
L McKeehan
L McKeehan on 16 Sep 2021
Thank you so much! now i totally get it!

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!