Can someone either point me to an online/free MATLAB compiler/interpreter or explain what happens in this MATLAB code?

Mike Webb picture Mike Webb · Dec 18, 2012 · Viewed 56.4k times · Source

I apologize in advance for this question. I don't like "explain this code" questions, but I can't find any documentation about my specific example.

Background
My issue is this. I am trying to translate some MATLAB code to C#, but I am at the same time having to learn MATLAB. I do not work with anyone that knows MATLAB code, I have no access to MATLAB, so I can't test any of the code, and I can't find any documentation on the following question. So...

Question(s)

  1. Is there a free online/desktop MATLAB compiler/interpreter somewhere that I can use to test out MATLAB code?

    ...or...

  2. Is there someone that can explain the following code snippet:

    someVar.member1=myValue1;
    someVar.member2=myValue2;
    if (myCondition)
        for i=1:myTotal
            someVar(i).member3=myValue3;
        end;
    end;
    

    Does this make someVar into an array? Do I lose member1 and member2 or does it save what I have set somehow?

Answer

Jonas picture Jonas · Dec 18, 2012

Re: 1 - There is the excellent Matlab Documentation, including video tutorials, which will help you understand Matlab. This is much more useful than a compiler, since you'll learn what the code intended, so that you can re-write it in a fashion that is appropriate for C#, rather than trying to copy Matlab-optimized syntax.

However, to test-run Matlab code, there is Octave which provides most of the functionality of core Matlab, but may not support toolbox functions (additional modules of Matlab that you pay for extra).

Re: 2 - Here's what the code does

Instantiate a structure array someVar (Matlab doesn't need declaring variables beforehand) with a field member; assign it to myValue1

someVar.member1=myValue1;

Create an additional field member2, set it to myValue2

someVar.member2=myValue2;

If the condition is true, loop myTotal times, and set the field member3 of all i elements of someVar to myValue3. Thus, someVar goes from a 1-by-1 structure array to a 1-by-myTotal structure array. someVar(1).member1 remains myValue1, while someVar(i).member1 are initialized to empty ([]).

if (myCondition)
    for i=1:myTotal
        someVar(i).member3=myValue3;
    end;
end;

/aside: This loop is a rather inefficient way to define the structure. So there may not be much Matlab-optimized syntax in the code you need to translate.