Slow execution of scripts

1 view (last 30 days)
Aritra Sasmal
Aritra Sasmal on 28 Jun 2018
Edited: Jan on 28 Jun 2018
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?

Accepted Answer

Jan
Jan on 28 Jun 2018
Edited: Jan on 28 Jun 2018
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.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!