How to Comment out lines (vary with different loops) using another code ?
3 views (last 30 days)
Show older comments
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 Comments
Stephen23
on 8 Mar 2016
Edited: Stephen23
on 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
on 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?
Answers (1)
Jan
on 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 Comments
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!