Hi, I would appreciate if someone could help me out with this. I have a finite element code where I use properties of different materials. These properties are stored in a prop.m file like this:
___________________________
---------*prop.m*----------------
length=1*x; width=2.5*x; height=6.7*x^2; etc
________________________
Now inside the FE code, I need to call this script many times from inside other functions (F_n, n=function number) like run('./prop.m') to give me the properties at the "x" value. I have noticed that the code runs woefully slow when the properties are coded like this and runs much faster when the properties are hardcoded inside the functions. However, I have a lot of different functions that use the same properties and hardcoding it inside each function is not an option. I do realize that running a script is much slower than running a function. Is there a nice way to get the properties inside F_n without having to "run" a script?
One way I have thought of is passing an object from each function such that the parameters are properties of the object. So in F_n, I will use:
Obj=prop(x); length=Obj.length; width=Obj.width; height=Obj.height; etc.
Will this be any faster?

 采纳的回答

Jan
Jan 2018-6-28
编辑:Jan 2018-6-28
I assume the function will be faster than the script. Simply try it.
Instead of replying one struct, you can reply a list of variables also:
function [length, width, height] = prop(x)
length=1*x;
width=2.5*x;
height=6.7*x^2;
end
Or
function Obj = prop(x)
Obj.length=1*x;
Obj.width=2.5*x;
Obj.height=6.7*x^2;
end
Both has advantages.
By the way: Do not use "length" as name of a variable, because this shadows an important built-in function.

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by