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

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 comentarios

Functions have their own workspace; they know only of variables either created internally or passed in by the argument list.
If you want to use variables speed and consumption inside the function, then you must pass them to it -- and, if you have an array named speed, then you can't name a function speed as well; whichever is the last to be defined will alias the other.
If you're reading the two arrays in the main program, unless you're going to be doing something more inside your function you may as well just call interp1 in line -- otherwise, the function would look like
function amount=fuel(speed,consumption,t)
amount=interp1(speed,consumption,t);
end
where I inferred what the output would be given the two array names as you defined them.
As you see, you've not really done anything in this case by creating a function you couldn't have done in the one line inline using interp1 in the calling code; it's where there's a series of operations that are complex or closely related that it makes sense to create a function for them to reduce clutter in the higher-level code.
"fixed the variable names since I wrote i wrong the first time)"
Whatever the variables in the workspace are named doesn't matter; a function has its own context and workspace and knows nothing about those variables in the calling workspace.
Reread the answer provided in detail and also read the documentation for <base-and-function-workspaces> which says in part "Functions do not use the base workspace. Every function has its own function workspace"
OK. I tried to "load" the matfiles in the function as @madhan ravi suggested but I got another error. This time saying: "Unable to read file 'time'. No such file or directory."
I made sure the file is in the current folder
Nvm. I got it to work :)
dpb
dpb el 14 de Feb. de 2021
Editada: dpb el 14 de Feb. de 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.

Iniciar sesión para comentar.

 Respuesta aceptada

madhan ravi
madhan ravi el 14 de Feb. de 2021
Editada: madhan ravi el 14 de Feb. de 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 comentarios

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 el 14 de Feb. de 2021
Editada: dpb el 14 de Feb. de 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.

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 14 de Feb. de 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.

Categorías

Preguntada:

el 14 de Feb. de 2021

Editada:

dpb
el 14 de Feb. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by