Initialize Variables in a Function
182 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
In a SCRIPT, I'm able to initialize variables which is displayed as empty variables in the workspace:
mass = [];
speed = [];
velocity = [];
BUT when I place these same initialize variables in a FUNCTION, MATLAB does not recognize them and does not store them in the workspace.
function myvariables()
mass = [];
speed = [];
velocity = [];
How can I execute initialize variables in a function?
Thanks, Amanda
0 comentarios
Respuesta aceptada
Iain
el 30 de Mayo de 2013
If you want a function to return those variables to the workspace, you'll need to declare it as:
function [mass speed velocity] = myvariables()
and call it as
[mass speed velocity] = myvariables()
Alternatively, you can assign those variables in either the calling, or base workspace (look up the help for assignin or evalin)
Alternatively, again, you can declare the variables as global in every function you want to use them, AND in the base workspace.
1 comentario
Iain
el 30 de Mayo de 2013
Variables declared within functions, and not explicitly returned in some fashion will be deleted when the function finishes, but are available within the function.
Más respuestas (0)
Ver también
Categorías
Más información sobre Variables en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!