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

조회 수: 188 (최근 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
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 CenterFile Exchange에서 Variables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by