What's the difference between % and %% for comments?

前端 未结 3 1064
傲寒
傲寒 2021-02-13 06:35

The MATLAB editor automatically highlights all content after %% comments, and text after %% in the same line are turned bold. But what\'s the essential

相关标签:
3条回答
  • 2021-02-13 06:42

    From a syntax point of view, they are both comments.

    In the Matlab editor, Matlab parses %% delimited blocks as "sections" which you can run as a unit independent of running the whole script.

    0 讨论(0)
  • 2021-02-13 06:51

    One percent sign (%) is used for commenting lines.

    Two percent signs (%%) have a different purpose: they are used for dividing your code into sections, which can be run independently. This allows easier debugging.

    0 讨论(0)
  • 2021-02-13 06:57

    I really like the double percent sign (%%) and use as far as possible for the following reasons:

    1. Creates a cell block which could be run separately from the whole code (Ctrl + Enter).

    2. As mentioned in sections, it improves the readability of the file and appears as a heading if you publish your code. It increases concentration by creating a yellow background and you can focus more on the part that you are working on.

    3. You can fold the code in cell blocks. (First you should enable code folding of cell blocks in Preferences >> Editor/Debugger >> Code Folding >> Sections). This is useful specially in large mfiles.

    4. If you care about keeping a clean Command History running the codes in cell blocks (Ctrl + Enter) does not leave any trace in Command History , unlike the Evaluate Selection (F9) which evaluates the selected (highlighted) code and holds the executed code in Command History.

    Hope it helps.

    0 讨论(0)
提交回复
热议问题