Why do I get the error "Unrecognized function or variable" with this

151 ビュー (過去 30 日間)
Buttercup12
Buttercup12 2021 年 2 月 14 日
編集済み: dpb 2021 年 2 月 14 日
I'm trying to create a function that gives the value of y given any x. I have two matrixes (speed1 and time) that shows corresponding values for the x- and y-axis, so I need my function to calculate the value of y given any x. To do this I have made the following:
function v=speed(t)
x=time;
y=speed1;
v=interp1(x,y,t);
end
But I get an error saying: "Unrecognized function or variable 'time'" (the same for speed1), and I don't understand what's wrong. The matrixes I used are saved matfiles that I have on my computer so to use them I first import the data so I have them in workspace and they work if I don't use them in a function.
What can I do to resolve the issue?
(edit: fixed the variable names since I wrote i wrong the first time)
  5 件のコメント
Buttercup12
Buttercup12 2021 年 2 月 14 日
Nvm. I got it to work :)
dpb
dpb 2021 年 2 月 14 日
編集済み: dpb 2021 年 2 月 14 日
Well, clearly the file you asked for, specifically, wasn't there. We can't see your console from here so can't know what you did without code and error message in context and in its entirety.
What does
whos -file time
return at the command line? Or/And
dir t*.mat
?
But, even if you fix this, from a program structure standpoint it still isn't a very good solution -- this way, every time you want a new value you have to reread the file from disk; a very overhead-intensive operation in place of reading the file once and passing it to functions that need it.
Plus, even if you were to add the code to make the two arrays persistent in the function and only read the file once, the function is only useful for the specific file referred to internally; to use a different dataset would require overwriting those two files with new ones of the same name.
All in all, it just is not a good factoring of the code; for what you're trying to do here, the solution of simply calling interp1 in line in the top level code is clearly the better factorization.

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

採用された回答

madhan ravi
madhan ravi 2021 年 2 月 14 日
編集済み: madhan ravi 2021 年 2 月 14 日
I don't know why you deleted your question and changed your name.
1) Why do you name a variable as the same name of a FUNCTION.
2) Call a function instead of pressing the green button to run a function which expects input arguments.
function v = SPEED(t)
load speed
load consumption
x = speed
y = consumption
v = interp1(x, y, t);
end
  2 件のコメント
Buttercup12
Buttercup12 2021 年 2 月 14 日
1) Sorry. Wrote it wrong. It's supposed to be:
function v=speed(t)
x=time
y=speed1
v=interp1(x,y,t);
end
2) That's what I've been doing.
dpb
dpb 2021 年 2 月 14 日
編集済み: dpb 2021 年 2 月 14 日
The function by whatever name still has the same problem -- time and speed1 are NOT arguments to the function so they are totally unknown inside the function.
I REPEAT -- FUNCTIONS HAVE THEIR OWN CONTEXT/WORKSPACE; THEY KNOW NOTHING OF VARIABLES IN THE CALLING WORKSPACE.
See
doc function
for details.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2021 年 2 月 14 日
What is time? Your function does not know because you did not pass it in. ALso, you did not pass in speed1 so it won't know that that is and won't be able to assign it to y. Why do you think your function should know what these are unless you pass them in? It won't, so they are "undefined" and the error message tells you that.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by