How do I pass variables between functions
Mostrar comentarios más antiguos
This should be trivial, but I keep getting errors.
This is my main function. It calls each function to read data from a .txt file.
function [] = int_main
%opens example file and calls all functions to read from it
FEA=fopen('FEA.txt');
readMesh(FEA);
readProperties(FEA);
readConstraints(FEA);
readLoads(FEA);
assembleGlobalStiffnessMatrix()
end
This is an example of one of the functions.
function [Properties] = readProperties(FEA)
%Reads the material properties about the beam
format shorteng
Properties= fscanf(FEA,'%*s %*s %*s %g %g %g',[3,1])'
end
Now, the problem is when I try to use the "Properties" variable in the assembleGlobalStiffnessMatrix. It is not in the same workspace.
function [] = assembleGlobalStiffnessMatrix(Properties)
% Sets up Stiffness Matrix
Properties
end
All I need to do is use the variable I calculated from the previous function. Other answers have suggested using a function function, but I don't think this will work because executing the readProperties function in the globalStiffnessMatrix function will cause the fscanf command to read the wrong line.
Somehow, I need to access the "Properties" variable. But my assignment specifies that global variables are prohibited. So, I have no idea how to make this work.
Thank you
2 comentarios
jgg
el 6 de Jul. de 2016
You have to pass things into the function you want and store output you want to store. So, in your workflow, you'd want something like this:
function [] = int_main
%opens example file and calls all functions to read from it
FEA=fopen('FEA.txt');
readMesh(FEA);
Properties = readProperties(FEA);
readConstraints(FEA);
readLoads(FEA);
assembleGlobalStiffnessMatrix(Properties)
end
Michael cornman
el 6 de Jul. de 2016
Respuestas (1)
Honglei Chen
el 6 de Jul. de 2016
It seems you didn't explicitly return and pass in the properties you computed? Could you try
properties = readProperties(FEA);
assembleGlobalStiffnessMatrix(properties)
in your main function?
HTH
Categorías
Más información sobre Whos en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!