Undefined function or variable

1 回表示 (過去 30 日間)
aklapaz
aklapaz 2017 年 3 月 20 日
コメント済み: aklapaz 2017 年 3 月 21 日
Hello, I am new to Matlab and been trying to solve the below task: Write a function called light_speed that takes as input a row vector of distances in kilometers and returns two row vectors of the same length. Each element of the first output argument is the time in minutes that light would take to travel the distance specified by the corresponding element of the input vector. To check your math, it takes a little more than 8 minutes for sunlight to reach Earth which is 150 million kilometers away. The second output contains the input distances converted to miles. Assume that the speed of light is 300,000 km/s and that one mile equals 1.609 km.
So my function looks like that:
function [ Time,DistM ] = light_speed(A)
[~,n]=size(A);
Time = A(1,1:n).*(1000/18000000);
DistM = A(1,1:n).*0.621371192 ; end
However Matlab comes with this message: >> edit light_speed
>> M=[1 2 3] M = 1 2 3
>> light_speed(M)
Undefined function or variable 'light_speed'.
Why it can't recognize the function at all even though I have saved it?
Thanks

採用された回答

Beder
Beder 2017 年 3 月 20 日
Did you save light_speed in the same .m file as the one that you're calling from?
a Code like the one below should work. Is your code structured like this?
a=1
b=myfunction(a)
function x=myfunction(a)
x=a+1;
end
  1 件のコメント
aklapaz
aklapaz 2017 年 3 月 21 日
Thank you.I had to save it again. How do we specify a vector with the size function? Is my solution correct? [~,n]=size(A);

サインインしてコメントする。

その他の回答 (1 件)

Adam
Adam 2017 年 3 月 20 日
You need to make sure it is on your path
doc addpath
or use the ribbon on the MAtlab GUI or just save it in your working directory.

カテゴリ

Help Center および File ExchangeSources についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by