1

First, I have had a look at this excellent article already. I have a MATLAB script, called sdp. I have another MATLAB script called track. I run track after sdp, as track uses some of the outputs from sdp. To run track I need to call a function called action many many times. I have action defined as a function in a separate MATLAB file. Each call of this action has some inputs, say x1,x2,x3, but x2,x3are just "data" which will never change. They were the same in sdp, same in track, and will remain the same in action. Here, x2,x3 are huge matrices. And there are many of them (think like x2,x3,...x10)

The lame way is to define x2,x3 as global in sdp and then in track, so I can call action with only x1. But this slows down my performance incredibly. How can I call action again and again with only x1 such that it remembers what x2,x3 are? Each call is very fast, and if I do this inline for example, it is super fast.

Perhaps I can use some persistent variables. But I don't understand exactly if they are applicable to my example. I don't know how to use them exactly either.

bissi
  • 65
  • 7

2 Answers2

0

Have a look at object oriented programming in Matlab. Make an action object where you assign the member variables x2 ... to the results from sdp. You can then call a method of action with only x1. Think of the object as a function with state, where the state information in your case are the constant results of sdp.

Another way to do this would be to use a functional approach where you pass action to track as a function handle, where it can operate on the variables of track.

learnvst
  • 15,455
  • 16
  • 74
  • 121
  • Would the object oriented programming approach be faster? Am not sure. Are you familiar with persistent variables or nesting these functions, perhaps that is related? – bissi Apr 20 '17 at 17:36
0

Passing large matrices in MATLAB is efficient. Semantically it uses call-by-value, but it's implemented as call-by-reference until modified. Wrap all the unchanging parameters in a struct of parameters and pass it around.

params.x2 = 1;  
params.x3 = [17 39];
params.minimum_velocity = 19;

action('advance', params);

You've already discovered that globals don't perform well. Don't worry about the syntactic sugar of hiding variables somewhere... there are advantages to clearly seeing where the inputs come from, and performance will be good.

This approach also makes it easy to add new data members, or even auxiliary metadata, like a description of the run, the time it was executed, etc. The structs can be combined into arrays to describe multiple runs with different parameters.

Peter
  • 14,559
  • 35
  • 55
  • Is this struct of parameters just an aesthetic construct, or does it positively impact performance too? Isn't this the same as just passing x2 and x3 but hiding them in the params parameter? If I paste the action into my script file tracking, then the code runs in a flash. Calling action multiple times slows it down incredibly.. – bissi Apr 21 '17 at 14:09
  • That sounds like function call overhead. Parameter passing is probably not even the issue. Remember that functions optimize better than scripts, so be sure everything is a function. – Peter Apr 21 '17 at 14:53