How to save variables in a function to the workspace

조회 수: 12 (최근 30일)
Hiroyasu Fujita
Hiroyasu Fujita 2021년 2월 12일
댓글: Hiroyasu Fujita 2021년 2월 13일
i want to save the variables( x , y , z , xx , yy) in a function to the workspace.
**************************************
global x
global y
global z
global xx
global yy
test(1,2,3)
function test(x,y,z)
x
y
z
x+y+z
test2(4,2)
end
function test2(xx,yy)
xx
yy
xx-yy
end

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 2월 12일
Don't use globals. Return your variables through the function output. See the documentation. This example from that page shows how to return the variable ave.
myave = average([1:10])
myave = 5.5000
% function declaration
function ave = average(x)
ave = sum(x(:))/numel(x);
end
  댓글 수: 9
Stephen23
Stephen23 2021년 2월 13일
@Hiroyasu Fujita: how to call functions with one or more outputs is explained here:
Those tutorials explain many basic concepts that you will need to know if you want to use MATLAB.
Hiroyasu Fujita
Hiroyasu Fujita 2021년 2월 13일
@Stephen Cobeldick , @Walter Roberson @Cris LaPierre
Thank you for your comments.
The problem has been solved.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by