How to save variables in a function to the workspace

33 vues (au cours des 30 derniers jours)
Hiroyasu Fujita
Hiroyasu Fujita le 12 Fév 2021
Commenté : Hiroyasu Fujita le 13 Fév 2021
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

Réponse acceptée

Cris LaPierre
Cris LaPierre le 12 Fév 2021
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 commentaires
Stephen23
Stephen23 le 13 Fév 2021
@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 le 13 Fév 2021
@Stephen Cobeldick , @Walter Roberson @Cris LaPierre
Thank you for your comments.
The problem has been solved.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with Phased Array System Toolbox dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by