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

152 Ansichten (letzte 30 Tage)
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 Kommentare
dpb
dpb am 14 Feb. 2021
Bearbeitet: dpb am 14 Feb. 2021
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

madhan ravi
madhan ravi am 14 Feb. 2021
Bearbeitet: madhan ravi am 14 Feb. 2021
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 Kommentare
Buttercup12
Buttercup12 am 14 Feb. 2021
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 am 14 Feb. 2021
Bearbeitet: dpb am 14 Feb. 2021
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 14 Feb. 2021
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.

Kategorien

Mehr zu Variables finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by