Why do I get the error "Unrecognized function or variable" with this
Mostrar comentarios más antiguos
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
dpb
el 14 de Feb. de 2021
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.
dpb
el 14 de Feb. de 2021
"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"
Buttercup12
el 14 de Feb. de 2021
Buttercup12
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.
Respuesta aceptada
Más respuestas (1)
Image Analyst
el 14 de Feb. de 2021
0 votos
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
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!