# How to Comment out lines (vary with different loops) using another code ?

4 visualizzazioni (ultimi 30 giorni)
Aravind Sasikumar il 8 Mar 2016
Commentato: Image Analyst il 8 Mar 2016
Hello All,
I just wanted to know whether there is a way to comment out lines on a file using another code. For example, I have file 1 and file 2. I use loops in file 2 which make it to run file 1 n number of times. For each n, i want different lines in file 1 to be commented out. Like for n=1, i want the 4th line to be commented and consider the 5 th line, and for n=2. I want the 4th line to be taken and 5th line to be commented out. It goes on like this. I know there are solutions apart from commenting lines to make it happen, but I am just curious if this can be done. ?
##### 2 CommentiMostra NessunoNascondi Nessuno
Stephen23 il 8 Mar 2016
Modificato: Stephen23 il 8 Mar 2016
"I am just curious if this can be done" Of course it can be done by reading and writing files.
But it would be an incredibly slow and buggy way to implement an algorithm. Write the code properly as functions and use input arguments to select the parts that need to run, or by masking the output, or using any other more efficient solution.
Image Analyst il 8 Mar 2016
This is very confusing: "Like for n=1, i want the 4th line to be commented and consider the 5 th line, and for n=2. I want the 4th line to be taken and 5th line to be commented out." I know what "commented out" means - you want to put a % at the beginning of the line. But exactly what does "consider" and "taken" mean? Should line 5 be considered for commenting out, or not? Again, define "consider" and define "taken". Then you need to specify the starting line, the line increment, and the ending line over which this commenting out will be performed.
So file2.m is going to comment out lines in file1.m. If n=1, tell me exactly what line numbers should have a % at the beginning of the line. Same for n=2. All I can tell now is that for either n=1 or n=2 you want both lines 4 and 5 commented out - or possibly not, who knows?

Accedi per commentare.

### Risposte (1)

Jan il 8 Mar 2016
It is possible. But you should not create a program, which modifies its source code.
Reading an M-file from the disk and parsing it takes a remarkable chunk of time, the first time it is called - magnitude of 0.1 sec. If the code stops with an error, you would need a very smart technique to control the current state of a code.
Summary: DON'T DO THIS!
Better control the program flow by arguments and switch statements. This is much cleaner and faster, esy to debug and not severely ugly.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Entering Commands in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by