Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Mar 17, 2009 #1
    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.
     
  2. jcsd
  3. Mar 17, 2009 #2

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    put a % sign at the beginning of the lines you wish to skip....
     
  4. Mar 18, 2009 #3
    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?
     
  5. Mar 18, 2009 #4

    MATLABdude

    User Avatar
    Science Advisor

    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.
     
  6. Mar 18, 2009 #5
    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!
     
  7. Mar 24, 2009 #6

    MATLABdude

    User Avatar
    Science Advisor

    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 (Text):
    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 (Text):
    if switchVariable==TRUE
    ...
    end
    Code (Text):
    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: MATLAB: Running only a certain section of an m-file
  1. MATLAB and M-Files (Replies: 4)

  2. MATLAB - geometry M-file (Replies: 20)

Loading...