how to count uncommented ligns in matlab file?

조회 수: 5 (최근 30일)
Louis Tomczyk
Louis Tomczyk 2023년 4월 2일
댓글: Adam Danz 2023년 4월 4일
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.

채택된 답변

Ive J
Ive J 2023년 4월 2일
편집: Ive J 2023년 4월 4일
% 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))
3 lines were found!
  댓글 수: 7
Ive J
Ive J 2023년 4월 4일
편집: Ive J 2023년 4월 4일
@Adam Danz a little bit of tweaking would take care of that as well. Check it out now 😊.
Adam Danz
Adam Danz 2023년 4월 4일
👏

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2023년 4월 2일
You can use my comment counter program, attached.
  댓글 수: 3
DGM
DGM 2023년 4월 3일
편집: DGM 2023년 4월 3일
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')
The top level folder is "/MATLAB/toolbox/images/colorspaces/". FILENAME COMMENT LINES COMMENT CHARACTERS SYNOPSIS LINES SYNOPSIS CHARACTERS Contents.m 46 / 48 ( 95.8%) 2405 / 2405 (100.0%) not a function file not a function file applycform.m 67 / 162 ( 41.4%) 3144 / 5698 ( 55.2%) 45 ( 27.8% of total 67.2% of comments) 1958 ( 34.4% of total 62.3% of comments) chromadapt.m 20 / 127 ( 15.7%) 738 / 3753 ( 19.7%) 0 ( 0.0% of total 0.0% of comments) 0 ( 0.0% of total 0.0% of comments) colorangle.m 3 / 28 ( 10.7%) 66 / 602 ( 11.0%) 0 ( 0.0% of total 0.0% of comments) 0 ( 0.0% of total 0.0% of comments) deltaE.m 8 / 43 ( 18.6%) 268 / 1155 ( 23.2%) 0 ( 0.0% of total 0.0% of comments) 0 ( 0.0% of total 0.0% of comments) iccfind.m 33 / 75 ( 44.0%) 1420 / 2227 ( 63.8%) 26 ( 34.7% of total 78.8% of comments) 1048 ( 47.1% of total 73.8% of comments) iccread.m 415 / 1670 ( 24.9%) 15536 / 52418 ( 29.6%) 23 ( 1.4% of total 5.5% of comments) 981 ( 1.9% of total 6.3% of comments) iccroot.m 30 / 71 ( 42.3%) 1170 / 2068 ( 56.6%) 13 ( 18.3% of total 43.3% of comments) 473 ( 22.9% of total 40.4% of comments) iccwrite.m 492 / 1564 ( 31.5%) 18361 / 49207 ( 37.3%) 33 ( 2.1% of total 6.7% of comments) 1481 ( 3.0% of total 8.1% of comments) illumgray.m 116 / 233 ( 49.8%) 4993 / 8143 ( 61.3%) 103 ( 44.2% of total 88.8% of comments) 4540 ( 55.8% of total 90.9% of comments) illumpca.m 123 / 229 ( 53.7%) 5370 / 7765 ( 69.2%) 87 ( 38.0% of total 70.7% of comments) 3623 ( 46.7% of total 67.5% of comments) illumwhite.m 9 / 87 ( 10.3%) 251 / 2129 ( 11.8%) 0 ( 0.0% of total 0.0% of comments) 0 ( 0.0% of total 0.0% of comments) imcolordiff.m 32 / 175 ( 18.3%) 1515 / 6440 ( 23.5%) 0 ( 0.0% of total 0.0% of comments) 0 ( 0.0% of total 0.0% of comments) isicc.m 49 / 180 ( 27.2%) 1624 / 4499 ( 36.1%) 34 ( 18.9% of total 69.4% of comments) 1107 ( 24.6% of total 68.2% of comments) lab2double.m 40 / 59 ( 67.8%) 1700 / 2192 ( 77.6%) 39 ( 66.1% of total 97.5% of comments) 1657 ( 75.6% of total 97.5% of comments) lab2rgb.m 8 / 90 ( 8.9%) 332 / 2854 ( 11.6%) 0 ( 0.0% of total 0.0% of comments) 0 ( 0.0% of total 0.0% of comments) lab2uint16.m 40 / 57 ( 70.2%) 1687 / 2139 ( 78.9%) 39 ( 68.4% of total 97.5% of comments) 1644 ( 76.9% of total 97.5% of comments) lab2uint8.m 40 / 58 ( 69.0%) 1678 / 2123 ( 79.0%) 39 ( 67.2% of total 97.5% of comments) 1635 ( 77.0% of total 97.4% of comments) lab2xyz.m 33 / 75 ( 44.0%) 1071 / 2077 ( 51.6%) 32 ( 42.7% of total 97.0% of comments) 1027 ( 49.4% of total 95.9% of comments) lin2rgb.m 12 / 110 ( 10.9%) 584 / 2764 ( 21.1%) 0 ( 0.0% of total 0.0% of comments) 0 ( 0.0% of total 0.0% of comments) makecform.m 295 / 1115 ( 26.5%) 13583 / 39098 ( 34.7%) 167 ( 15.0% of total 56.6% of comments) 7871 ( 20.1% of total 57.9% of comments) ntsc2rgb.m 46 / 90 ( 51.1%) 1708 / 2701 ( 63.2%) 29 ( 32.2% of total 63.0% of comments) 1049 ( 38.8% of total 61.4% of comments) rgb2lab.m 7 / 78 ( 9.0%) 286 / 2465 ( 11.6%) 0 ( 0.0% of total 0.0% of comments) 0 ( 0.0% of total 0.0% of comments) rgb2lightness.m 29 / 57 ( 50.9%) 974 / 1624 ( 60.0%) 28 ( 49.1% of total 96.6% of comments) 931 ( 57.3% of total 95.6% of comments) rgb2lin.m 12 / 110 ( 10.9%) 584 / 2737 ( 21.3%) 0 ( 0.0% of total 0.0% of comments) 0 ( 0.0% of total 0.0% of comments) rgb2ntsc.m 40 / 77 ( 51.9%) 1430 / 2404 ( 59.5%) 23 ( 29.9% of total 57.5% of comments) 741 ( 30.8% of total 51.8% of comments) rgb2xyz.m 5 / 62 ( 8.1%) 192 / 1711 ( 11.2%) 0 ( 0.0% of total 0.0% of comments) 0 ( 0.0% of total 0.0% of comments) rgb2ycbcr.m 78 / 146 ( 53.4%) 3155 / 4760 ( 66.3%) 47 ( 32.2% of total 60.3% of comments) 1776 ( 37.3% of total 56.3% of comments) rgbwide2xyz.m 90 / 130 ( 69.2%) 3458 / 4877 ( 70.9%) 81 ( 62.3% of total 90.0% of comments) 3010 ( 61.7% of total 87.0% of comments) rgbwide2ycbcr.m 71 / 87 ( 81.6%) 2620 / 3080 ( 85.1%) 67 ( 77.0% of total 94.4% of comments) 2458 ( 79.8% of total 93.8% of comments) whitepoint.m 26 / 79 ( 32.9%) 1026 / 2086 ( 49.2%) 24 ( 30.4% of total 92.3% of comments) 974 ( 46.7% of total 94.9% of comments) xyz2double.m 34 / 51 ( 66.7%) 1264 / 1713 ( 73.8%) 33 ( 64.7% of total 97.1% of comments) 1221 ( 71.3% of total 96.6% of comments) xyz2lab.m 33 / 75 ( 44.0%) 1083 / 2089 ( 51.8%) 32 ( 42.7% of total 97.0% of comments) 1039 ( 49.7% of total 95.9% of comments) xyz2rgb.m 6 / 82 ( 7.3%) 207 / 2188 ( 9.5%) 0 ( 0.0% of total 0.0% of comments) 0 ( 0.0% of total 0.0% of comments) xyz2rgbwide.m 86 / 125 ( 68.8%) 3313 / 4745 ( 69.8%) 76 ( 60.8% of total 88.4% of comments) 2800 ( 59.0% of total 84.5% of comments) xyz2uint16.m 34 / 51 ( 66.7%) 1240 / 1692 ( 73.3%) 33 ( 64.7% of total 97.1% of comments) 1197 ( 70.7% of total 96.5% of comments) ycbcr2rgb.m 67 / 137 ( 48.9%) 2797 / 4359 ( 64.2%) 35 ( 25.5% of total 52.2% of comments) 1368 ( 31.4% of total 48.9% of comments) ycbcr2rgbwide.m 66 / 82 ( 80.5%) 2534 / 3007 ( 84.3%) 62 ( 75.6% of total 93.9% of comments) 2370 ( 78.8% of total 93.5% of comments) lab2rgb.m 114 / 300 ( 38.0%) 3728 / 8960 ( 41.6%) 1 ( 0.3% of total 0.9% of comments) 39 ( 0.4% of total 1.0% of comments) rgb2lab.m 107 / 292 ( 36.6%) 3577 / 8710 ( 41.1%) 1 ( 0.3% of total 0.9% of comments) 39 ( 0.4% of total 1.1% of comments) rgb2lightness.m 32 / 54 ( 59.3%) 1011 / 1474 ( 68.6%) 27 ( 50.0% of total 84.4% of comments) 927 ( 62.9% of total 91.7% of comments) rgb2ycbcr.m 23 / 127 ( 18.1%) 854 / 4146 ( 20.6%) 1 ( 0.8% of total 4.3% of comments) 41 ( 1.0% of total 4.8% of comments) rgbwide2ycbcr.m 5 / 16 ( 31.2%) 121 / 666 ( 18.2%) 1 ( 6.2% of total 20.0% of comments) 33 ( 5.0% of total 27.3% of comments) ycbcr2rgb.m 20 / 132 ( 15.2%) 741 / 4208 ( 17.6%) 1 ( 0.8% of total 5.0% of comments) 41 ( 1.0% of total 5.5% of comments) ycbcr2rgbwide.m 5 / 16 ( 31.2%) 121 / 670 ( 18.1%) 1 ( 6.2% of total 20.0% of comments) 33 ( 4.9% of total 27.3% of comments) FILENAME COMMENT LINES COMMENT CHARACTERS SYNOPSIS LINES SYNOPSIS CHARACTERS The following files have less than 10% of lines commented (low comment frequency) lab2rgb.m 8 / 90 lines ( 8.89%) rgb2lab.m 7 / 78 lines ( 8.97%) rgb2xyz.m 5 / 62 lines ( 8.06%) xyz2rgb.m 6 / 82 lines ( 7.32%) The following files devote less than 10% of characters to comments (low comment volume) xyz2rgb.m 207 / 2188 characters ( 9.46%) Done processing all folders! Found 45 files in 2 folders. Overall, 2947 out of 8712 lines (40.29%) had comments. Overall, 115520 out of 278828 characters (46.74%) represented comments. The average file contained 194 lines, 65 of which contained comments. The average file contained 6196 characters, 2567 of which represented comments. The average function file synopsis contained 29 lines, or 1162 characters
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.
Adam Danz
Adam Danz 2023년 4월 3일
Wow, that looks useful!

댓글을 달려면 로그인하십시오.

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by