Threading in matlab.

Discussion in 'Programmer's Corner' started by silverscorpion, Jan 14, 2008.

  1. silverscorpion

    Thread Starter New Member

    Dec 5, 2007
    2
    0
    hi,
    I'm building a robot with vision, which is controlled by computer. ie, the robot has a camera onboard, and the bot is also connected to the parallel port of computer. Matlab is used to control the bot. now, my question is, how to implement threading in matlab?

    I want matlab to perform two or more operations simultaneously. like, it has to keep track of time and take a snapshot every few seconds, say, 5 seconds. It also has to determine where the bot should go, simultaneously. How can i do it? Is there something like in matlab, equivalent to threading in java?

    Please help me. It's a bit urgent...
    Thanx.
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,177
    1,799
    I don't think so. Matlab is just an interpreter. It is very efficient at vector and matrix operations. It is horrible to the point of excruciating pain at anything resembling a control loop. Whatever appearence of simultaneity you can achieve you really don't get it without multiple processors.
     
  3. scotthirsch

    New Member

    Jan 15, 2008
    1
    0
    While MATLAB doesn't have an explicit threading model, this is pretty straightforward to do. If you are using the Data Acquisition Toolbox to connect to the robot with the parallel port, you can use the TimerFcn of the digitalio object.

    e.g.
    dio = digitalio('parallel');
    addline(dio,1,'out')
    set(dio,'TimerFcn','datestr(now)') % Point to your function - % set(dio,'TimerFcn',@TakeSnapshot)
    set(dio,'TimerPeriod',2)
    start(dio)

    If you are using some other means of connecting to the parallel port, you can use the TIMER object directly, which is built into MATLAB. It works pretty much the same, but gives some more control.

    t = timer;
    set(t,'TimerFcn','datestr(now)')
    set(t,'Period',2)
    set(t,'ExecutionMode','fixedRate')
    start(t)
     
  4. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    145
    Limited multi-threading capabilities are built into later versions of Matlab (at least v.2006 onwards), however from my personal use of this "feature" I find it is only of any use on multi-core processors (where the number of threads is matched to the number of cores). The observed benefits are not overwhelming.

    The problem with implementing multi-threading in Matlab is two-fold: 1) the interpreted nature of Matlab means any performance enhancements beyond standard vectorisation of code is limited by the interpreter throughput hence negating many of performance benefits of threading (scrotthirsch's recommendation above, whilst a suitable implementation, would befall this problem); and 2) the later versions of Matlab that have multi-threading capabilities are now so bloated that they are generally performance-crippled by design.

    Dave
     
  5. Papabravo

    Expert

    Feb 24, 2006
    10,177
    1,799
    I think my last use of Matlab was around 5 years ago so I'm not surprised that it has moved on. Regardless of the language used multiple threads offer no performance bnefit on a single core processor. You need multiple cores or something like a VLIW DSP from Texas Instruments.
     
  6. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    145
    If your interested in still using the Matlab language (or at least large portions of it) then consider having a look at GNU Octave which has just gone to version 3. It follows the same matrix-based concepts and calculations (inc. syntax), but without the ridiculous price-tag and all the Java-garbage that is tagged onto Matlab.

    Dave
     
  7. scubasteve_911

    Senior Member

    Dec 27, 2007
    1,202
    1
    Matlab will thread easily if you have more than one core to work with. When it is 'thinking', I see both cores at 100% in the system performance dialog.

    Steve
     
  8. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    145
    What's probably happening is one core is being wasted crunching on the Matlab bloat and the other is working on your calculations. I still don't see the benefit in threading Matlab calculations - its too slow for anything intensive.

    It's amazing to see how bad Matlab has become since the days of v5.

    Dave
     
Loading...