Variable disappears when using it
Mostrar comentarios más antiguos
Hi, I've imported rows and columns of data from an excel file. It shows up as a variable in my workspace. When I call the variable in the Command Window, it shows up. However, when I input or try to apply the variable in a matlab code in the editor window and hit "Run", the variable disappears when the simulation gets to that line of code.
order=3;
framelen=61;
y=sgolayfilt(var1,order,framelen)
,where var1 is the imported matrix data I am trying to use. New to Matlab and wondering if I am supposed to save the variable, call it, or load it into my code.
Thanks in Advance!
Respuesta aceptada
Más respuestas (1)
ES
el 20 de Mzo. de 2018
0 votos
Your var1 is in base workspace. So if your code is a Function, the var 1 is not available to the function because the function operates on its own workspace. In this case you have to explicitly read from base workspace using evalin.
If your code is a script, you check for "clear" in your code. MATLAB Script works on the base workspace, but a clear will clear variables from the base workspace.
https://in.mathworks.com/help/matlab/matlab_prog/scripts-and-functions.html
1 comentario
Stephen23
el 20 de Mzo. de 2018
"In this case you have to explicitly read from base workspace using evalin."
This is incorrect, bad advice. It is NOT necessary to use slow, complex, buggy evalin. The simplest and most efficient way of passing data between workspaces is to simply pass that data as input and output arguments. This is clearly explained in the MATLAB documentation (which all beginners should learn to read): "Best Practice: Passing Arguments"
Do NOT follow the advice of magically passing data using evalin, unless you want to force yourself into writing slow, buggy, complex code that is hard to debug. The MATLAB documentation specifically advises to avoid the method given in this answer: "these functions carry risks of overwriting existing data. Use them sparingly."
For more information on why using evalin causes more problems than is solves, read this page and all of the pages it links to:
Categorías
Más información sobre Workspace Variables and MAT Files 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!