Reputation: 9013
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)
Is there a free online/desktop MATLAB compiler/interpreter somewhere that I can use to test out MATLAB code?
...or...
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?
Upvotes: 20
Views: 59907
Reputation: 1078
I just want to reiterate one of the suggested choice: anycodex.com in a previous answer, because it wasn't mentioned that it's an actual online MATLAB interpreter, which is really nice. I was suspicious at first, thinking that it was just another one powered by Octave, but after using ver
command it seems to show that it uses MATLAB Version: 7.14.0.739 (R2012a)
as seen here with its installed products:
--------------------------------------------------------------------------------------------------
MATLAB Version: 7.14.0.739 (R2012a)
MATLAB License Number: 161052
Operating System: Linux 3.5.0-23-generic #35~precise1-Ubuntu SMP Fri Jan 25 17:15:33 UTC 2013 i686
Java Version: Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
--------------------------------------------------------------------------------------------------
MATLAB Version 7.14 (R2012a)
Simulink Version 7.9 (R2012a)
Aerospace Blockset Version 3.9 (R2012a)
Aerospace Toolbox Version 2.9 (R2012a)
Bioinformatics Toolbox Version 4.1 (R2012a)
Communications System Toolbox Version 5.2 (R2012a)
Computer Vision System Toolbox Version 5.0 (R2012a)
Control System Toolbox Version 9.3 (R2012a)
Curve Fitting Toolbox Version 3.2.1 (R2012a)
DO Qualification Kit Version 1.6 (R2012a)
DSP System Toolbox Version 8.2 (R2012a)
Database Toolbox Version 3.11 (R2012a)
Datafeed Toolbox Version 4.3 (R2012a)
Econometrics Toolbox Version 2.1 (R2012a)
Embedded Coder Version 6.2 (R2012a)
Filter Design HDL Coder Version 2.9.1 (R2012a)
Financial Derivatives Toolbox Version 5.9 (R2012a)
Financial Toolbox Version 4.2 (R2012a)
Fixed-Income Toolbox Version 2.3 (R2012a)
Fixed-Point Toolbox Version 3.5 (R2012a)
Fuzzy Logic Toolbox Version 2.2.15 (R2012a)
Global Optimization Toolbox Version 3.2.1 (R2012a)
HDL Coder Version 3.0 (R2012a)
HDL Verifier Version 4.0 (R2012a)
IEC Certification Kit Version 2.1 (R2012a)
Image Acquisition Toolbox Version 4.3 (R2012a)
Image Processing Toolbox Version 8.0 (R2012a)
Instrument Control Toolbox Version 3.1 (R2012a)
MATLAB Builder JA Version 2.2.4 (R2012a)
MATLAB Coder Version 2.2 (R2012a)
MATLAB Compiler Version 4.17 (R2012a)
MATLAB Distributed Computing Server Version 6.0 (R2012a)
MATLAB Report Generator Version 3.12 (R2012a)
Mapping Toolbox Version 3.5 (R2012a)
Model Predictive Control Toolbox Version 4.1 (R2012a)
Neural Network Toolbox Version 7.0.3 (R2012a)
Optimization Toolbox Version 6.2 (R2012a)
Parallel Computing Toolbox Version 6.0 (R2012a)
Partial Differential Equation Toolbox Version 1.0.20 (R2012a)
Phased Array System Toolbox Version 1.2 (R2012a)
RF Toolbox Version 2.10 (R2012a)
Robust Control Toolbox Version 4.1 (R2012a)
Signal Processing Toolbox Version 6.17 (R2012a)
SimBiology Version 4.1 (R2012a)
SimDriveline Version 2.2 (R2012a)
SimElectronics Version 2.1 (R2012a)
SimEvents Version 4.1 (R2012a)
SimHydraulics Version 1.10.1 (R2012a)
SimMechanics Version 4.0 (R2012a)
SimPowerSystems Version 5.6 (R2012a)
SimRF Version 3.2 (R2012a)
Simscape Version 3.7 (R2012a)
Simulink 3D Animation Version 6.1 (R2012a)
Simulink Code Inspector Version 1.1 (R2012a)
Simulink Coder Version 8.2 (R2012a)
Simulink Control Design Version 3.5 (R2012a)
Simulink Design Optimization Version 2.1 (R2012a)
Simulink Design Verifier Version 2.2 (R2012a)
Simulink Fixed Point Version 7.1 (R2012a)
Simulink Report Generator Version 3.12 (R2012a)
Simulink Verification and Validation Version 3.3 (R2012a)
Stateflow Version 7.9 (R2012a)
Statistics Toolbox Version 8.0 (R2012a)
Symbolic Math Toolbox Version 5.8 (R2012a)
System Identification Toolbox Version 8.0 (R2012a)
SystemTest Version 2.6.3 (R2012a)
Wavelet Toolbox Version 4.9 (R2012a)
Very nice find indeed. Cheers.
Upvotes: 1
Reputation: 6424
I've written a free online interface for MATLAB/Octave that runs scripts and also has a live prompt where you can type commands. You can also save your scripts between sessions. Check it out at octave-online.net.
Upvotes: 10
Reputation: 246
Try this website:
http://www.compileonline.com/execute_matlab_online.php
It has a Matlab compiler as well as many other languages such as Perl, Python, Java, C, etc.
Upvotes: 8
Reputation: 74940
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.
Upvotes: 17
Reputation: 24127
You can try Octave, which is free and pretty compatible with MATLAB.
At the end of this snippet (assuming myCondition
is true), someVar
will be a vector with length myTotal
(i.e. an array of size 1 by myTotal
). Each element of the vector will be a structure with three fields member1
, member2
and member3
. The first element of the vector will have values for all three fields; the remaining ones will have values only for member3
, and will have the empty array as values for member1
and member2
. If myCondition
is false, you'll have single structure with two fields member1
and member2
.
Hope that helps!
Upvotes: 8