필터 지우기
필터 지우기

Initialize Variables in a Function

조회 수: 115 (최근 30일)
Amanda
Amanda 2013년 5월 30일
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

채택된 답변

Iain
Iain 2013년 5월 30일
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
Iain
Iain 2013년 5월 30일
Variables declared within functions, and not explicitly returned in some fashion will be deleted when the function finishes, but are available within the function.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by