As per knowledge, your above stated direction is best.
- Save the code as new function file say fun1
- In the Main script use loop to run it multiple times
for i=1:10
result=fun1()
end
The output of the code can be save in array or cell array depending on output arguments of the function file