开发者

Running code in MatLab/Mathematica only after having written it all

开发者 https://www.devze.com 2022-12-13 03:43 出处:网络
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\"

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" butto开发者_JAVA百科n, 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号