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

In summary: Sounds like you want to call the file specifically when your code is executed.Yes, I would like to have my code called only when it is executed.
  • #1
chendry2001
7
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
  • #2
put a % sign at the beginning of the lines you wish to skip...
 
  • #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?
 
  • #4
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.
 
  • #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!
 
  • #6
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.
 

1. How do I run only a specific section of my MATLAB code?

To run only a specific section of an m-file, you can use the "run" or "runsection" function. This allows you to specify the starting and ending line numbers of the section you want to run. Alternatively, you can use the "cell mode" feature in the MATLAB editor to select and run specific sections of code.

2. Can I run multiple sections of code at once in MATLAB?

Yes, you can run multiple sections of code at once by using the "run" or "runsection" function with multiple line number inputs. You can also use the "cell mode" feature to select and run multiple sections of code simultaneously.

3. What happens if I try to run a section of code that is not valid?

If you try to run a section of code that is not valid (e.g. missing a closing bracket), MATLAB will display an error message and the code will not run. Make sure to check for any errors in your code before running specific sections.

4. Can I use conditional statements to run specific sections of code in MATLAB?

Yes, you can use conditional statements (e.g. "if" statements) to control which sections of code are run based on certain conditions. This can be useful for creating more dynamic and efficient code.

5. Is it possible to stop a section of code from running in the middle?

Yes, you can use the "stop" button in the MATLAB editor to stop a section of code from running in the middle of execution. This can be useful if you need to make changes to your code while it is running or if you encounter an error and want to stop the code from running further.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
20
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
572
  • Programming and Computer Science
Replies
4
Views
744
Replies
15
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top