How to use data in a script from a function in another.
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I'm new to matlab and am trying to learn functions. For example, I have this function saved in a folder:
%example function
function Bricks = UseBrickData
Bricks.Red.Height = 10;
Bricks.Yellow.Height = 20;
end
I create another general script in the same folder, this is the script I'm using to do my calculations. How do I use the data from the Bricks function in this new general script? How do I specifically use the Red Height and the Yellow Height in this new general script?
1 comentario
Stephen23
el 11 de Oct. de 2022
S = UseBrickData();
S.Red.Height
S.Yellow.Height
%example function
function Bricks = UseBrickData
Bricks.Red.Height = 10;
Bricks.Yellow.Height = 20;
end
Respuestas (1)
Les Beckham
el 11 de Oct. de 2022
Editada: Les Beckham
el 11 de Oct. de 2022
% Example script
NewBricks = UseBrickData(); % Call the function and assign the return value to the variable NewBricks
disp(NewBricks.Red.Height)
disp(NewBricks.Yellow.Height)
% example function - if you want to be able to call it from anywhere, save
% it in a file called UseBrickData.m instead of inside your script
function Bricks = UseBrickData()
Bricks.Red.Height = 10;
Bricks.Yellow.Height = 20;
end
Also (FWIW), since this function doesn't use the data, it creates it, you might want to call it CreateBrickData() or DefineBrickData().
0 comentarios
Ver también
Categorías
Más información sobre Data Acquisition Toolbox Supported Hardware 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!