Variable disappears when using it

29 visualizaciones (últimos 30 días)
Vicki Ly
Vicki Ly el 20 de Mzo. de 2018
Comentada: Vicki Ly el 21 de Mzo. de 2018
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

Stephen23
Stephen23 el 20 de Mzo. de 2018
Editada: Stephen23 el 20 de Mzo. de 2018
@Vicki Ly: the best answer ( best in the sense fastest, neatest, least buggy, best practice as recommended by the MATLAB documentation and all MATLAB experts) is to simply pass your data as input and output arguments. For this you will need to define your function with input and output arguments. See the function help for more information on how to do that:
The introductory tutorials teach how to call functions correctly (with input and output arguments), and are highly recommended for all beginners:
"However, when I input or try ... and hit "Run","
That awful "Run" button has caused a continuous stream of beginners so much confusion because they try to run some function which requires inputs but that badly designed "Run" button does not provide their function with its required inputs. The best thing that can happen is that TMW removes the "Run" button from the toolbar. Until then the best thing that you can do is to ignore that button. Do NOT use it. When you want to run code, simply execute it from the command line by typing the script name, or the function name (along with any required input and output arguments) and then press enter. Or call it from another script/function/callback/class/.... but do NOT use that button. How to run scripts/functions is also explained in the introductory tutorials, which I highly recommend that you work through to learn basic MATLAB usage.
  1 comentario
Vicki Ly
Vicki Ly el 21 de Mzo. de 2018
Thanks! This worked. Avoiding the "Run" button seemed to do the trick. I am able to import my data and run the script via Command Window. Much thanks again! =)

Iniciar sesión para comentar.

Más respuestas (1)

ES
ES el 20 de Mzo. de 2018
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
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:

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by