how to count uncommented ligns in matlab file?
    4 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Louis Tomczyk
 el 2 de Abr. de 2023
  
    
    
    
    
    Comentada: Adam Danz
    
      
 el 4 de Abr. de 2023
            Hi,
I would like to count the number of ligns of code I produced for a project.
But I don't want to count commented ligns in the file. Otherwise I know I can use the function suggested (here).
Moreover sometimes the first character is not the (%) and there are some blank spaces before
Does anyone have an idea?
Thanks a lot.
0 comentarios
Respuesta aceptada
  Ive J
      
 el 2 de Abr. de 2023
        
      Editada: Ive J
      
 el 4 de Abr. de 2023
  
      % doc readlines
% lines = readlines("myfunc.m");
lines = ["   % comment 1"
    "dummy = 1 + 3;"
    " "
    "%{"
    "this is "
    "an example "
    "%}"
    "print(dummy)"
    ""
    ""
    "%{"
    " yet another"
    "block"
    "%}"
    "myvar = rand(10, 1);"
    "%{ "
    "comment"
    "comment"];
lines = strtrim(lines);
% check comments blocks
idx_s = lines == "%{";
idx_e = lines == "%}";
if any(idx_s)
    if sum(idx_s) > sum(idx_e), idx_e(end) = true; end
    idx = arrayfun(@(x,y)(x:y), find(idx_s), find(idx_e), uni=false);
    idx = horzcat(idx{:});
    lines(idx) = [];
end
lines(lines == "" | lines.startsWith("%")) = [];
fprintf("%d lines were found!\n", numel(lines))
7 comentarios
  Ive J
      
 el 4 de Abr. de 2023
				
      Editada: Ive J
      
 el 4 de Abr. de 2023
  
			@Adam Danz a little bit of tweaking would take care of that as well. Check it out now 😊.
Más respuestas (1)
  Image Analyst
      
      
 el 2 de Abr. de 2023
        You can use my comment counter program, attached.
3 comentarios
  DGM
      
      
 el 3 de Abr. de 2023
				
      Editada: DGM
      
      
 el 3 de Abr. de 2023
  
			There's still this thing that I built off comment_counter.m to add support for legacy versions and some other features.    
This all comes from a conversation here:
thispath = fullfile(matlabroot,'toolbox/images/colorspaces/');
checkcomments('path',thispath,'direction','ascend','countblank','recursive')
Rik gets credit for the core code that actually finds the comments.   If all one needs to do is isolate comments from non-comments, then that's really what they need.  See checkcommProcessor() and Rik's minify code on the FEX.  
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




