Question

I'd like to know if there's a way that when using Matlab, instead of having it interpret what I write line by line, if allows me to write all I want, and only interpret it when I hit an "Evaluate" button, or something like that. Coming from c++/c# I like to write the code I have to, and only then run it.

Also I don't like it putting >>'s in the beggining of the line. Is there a way to just that take off?

I ask the same question in relationship to Mathematica. I heard there's a Wolfram's Workbench(that doesn't seem to be known at all by most of the persons) that does just that, but it doesn't seem to be given to universities, so I never tried it.

Was it helpful?

Solution

If you write your code in code files (.m extension) then you can run it all at once.

Run:

edit my_matlab_file

and then write your code in the editor. Save the file. To run what you just coded you have a few options:

  1. In the command line do

    my_matlab_file

  2. In the editor press the "Evaluate" button (little green thingy)

  3. In the editor, press Ctrl+ENTER.

For more control you can also divide your file into cells which can be evaluated separately using Ctrl+ENTER:

my_matlab_file.m:

%% Initialization (Cell 1)
x = 1;
y = 2;

%% Calculation (Cell 2)
z = x + y

This is really useful when you have a long file that takes a long time to execute and you have to make changes somewhere. Instead of rerunning everything you can evaluate only the cell where you made your updates.

.m-files can also be used to create functions. Example (mymeanfund.m)

function y = mymeanfunc(x)
% Y = MYMEANFUNC(X) calculates the mean of X

y = sum(X(:)) / numel(X)

and run it by calling it:

>> m = mymeanfunc([1 2 3 4])
m = 2.5

As a side note, since more recent versions of MATLAB it is also perfectly possible to develop using OOP.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top