MATLAB: Running only a certain section of an m-file

Click For Summary

Discussion Overview

The discussion revolves around how to run specific sections of an m-file in MATLAB without modifying the original file. Participants explore various methods to skip certain lines and execute only the necessary code, focusing on practical solutions for maintaining the integrity of the original file while implementing an addon.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant inquires about skipping the first and last 10 lines of an m-file without altering it, seeking advice on how to achieve this.
  • Another participant suggests using the % sign to comment out lines, but acknowledges that this would modify the original file.
  • A different participant proposes copying the pertinent code into a new m-file as a solution to avoid changes to the original file.
  • One participant discusses the possibility of using if statements to conditionally execute code blocks, depending on input parameters or variable states.
  • Another suggestion involves creating a separate m-file for the parts that need to be stripped out, which would only be called when necessary.

Areas of Agreement / Disagreement

Participants do not reach a consensus on a single method to achieve the desired outcome. Multiple competing views and approaches are presented, with no clear resolution on the best solution.

Contextual Notes

Participants express concerns about modifying the original m-file and the implications this has for future updates. The discussion highlights the flexibility of MATLAB in terms of code execution without recompilation.

chendry2001
Messages
7
Reaction score
0
Hi

I have an m-file which accesses another m-file at some point, but I don't want it to run the entire file. Is there a way to skip the first and last 10 lines of the file?
I could just create a new file with the code I need, but this is like an addon to a programme, so if I could leave the original untouched that would be best.
Hope that makes sense. Any advice much appreciated!
Thanks.
 
Physics news on Phys.org
put a % sign at the beginning of the lines you wish to skip...
 
Yes, that would work, but again I would be changing the original file which I'm trying to avoid. Is there something like a "goto" command or similar?
 
chendry2001 said:
Yes, that would work, but again I would be changing the original file which I'm trying to avoid. Is there something like a "goto" command or similar?

No. Why not just copy and paste the pertinent code section into a new m-file? You wouldn't be modifying the original in that case.

EDIT: Or you can copy the entire file and then comment out the first and last sections. Really, the beauty of MATLAB is that you don't need to be constantly recompiling and regenerating binaries. Stuff just happens on the fly.
 
Think that's what I'll do. I'm creating an addon to an existing piece of code, so ideally people could update that original m-file and my addon would still work fine.
As I am going to create a copy of the original code, 2 m-files will now have to be updated in order for my addon to work.
Doesn't really matter though.

Thanks for your help!
 
I suppose another thing you could do is to put in some if statements around the pertinent code blocks. If the m-file is a function, you can put in an extra input (and either look at the input or use the nargin function)

Code:
if nargin > 3  %(or maybe something like variable3=='DEBUG')
 ...
end

If the m-file is just a code block, with no input or outputs, you can do the same thing using something like, say,

Code:
if switchVariable==TRUE
...
end

Code:
if exist debugVariable %define debugVariable when needed
...
end
More on the exist function:
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/exist.html

That or to strip out the parts at the beginning and end and put them into another m-file which is called only when you need them.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 18 ·
Replies
18
Views
6K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K