MATLAB Answers

Starting Code from Middle

3 views (last 30 days)
Joe Winkler
Joe Winkler on 13 Oct 2021
Edited: John D'Errico on 13 Oct 2021
I have a fairly long piece of code which involves some manual selection of points that I am testing which someone else created. I would like to be able to start that code from a given line in the middle. I had a collegue show me how to save the code/variables at a given line (after the manual selection of points) then highlight the remaining code and use the "Evaluate Selection" command to run it from there. But this does not work for me, any ideas why?

Answers (1)

John D'Errico
John D'Errico on 13 Oct 2021
Edited: John D'Errico on 13 Oct 2021
Why? Because you are not doing it correctly? What can I say? Maybe you are just running a really old version of MATLAB. Most likely, you just don't understand how to use these capabilities of MATLAB.
I'll asume this is a script.
First, split the code into pieces. Do that by inserting a blank line in the script, with a double % symbol at the beginning of the line. Put the line where you want to break the code into segments. Like this:
So your code might now look like this:
% My example script
x = 1:5;
% first cell
% second cell
Ok. I'll assume your script is more complicated than that, but who cares? :)
Now, in the editor, if I click the mouse inside the first segment that entire piece becomes hilighted in yellow. At the top of the editor window, the bar will have a button that is called "Run Section". If I click on run section, then it runs that section, and ONLY that section or block of code. In fact, I can run it again. And any variables that exist in your workspadce are available for use there.
Or, I can click on "Run and Advance". That will run the current section, but then it will highlight the next block of code.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by