10

How can I word-wrap long lines of scripts and functions in the MATLAB R2011b Editor?

I don't mean using Ctrl+J to wrap comments, but show a long line over several lines, each (except the last) the width of the editor frame, as in most text editors, so I don't need to scroll horizontally; the effect is purely visual and it remains as a single line in the file.

I could not find a setting in any menu, toolbar or the preferences dialog.

Though I could split the line into multiple lines using "...", I don't want to do this. I want to keep it as one line in the file, just show it wrapped, as in Format > Word Wrap in Windows Notepad.

Gnubie
  • 2,587
  • 4
  • 25
  • 38
  • 1
    MATLAB's editor has 3 very useful features I wish Notepad++ had: 1. Highlighting every occurrence of the selected variable on a bar next to the scrollbar; 2. Tooltip showing contents of a variable by just pointing at its name; 3. Automatic renaming of variables (eimilar to Visual Studio) http://blogs.mathworks.com/desktop/2011/09/12/variable-and-function-rename/ – Gnubie May 02 '12 at 12:15
  • (1) `m-x occur` (2) ..okay, emacs can't deal with that :O (3) `m-x replace-string`. I've used emacs with matlab-mode for quite some time now and I love it. – Dang Khoa May 02 '12 at 23:38
  • 1
    First off, please note that I've no intention to disparage MATLAB :) Re (1), as far as I've read, m-x occur opens a new buffer listing lines which contain the given string. MATLAB's editor instead has a vertical bar next to the scrollbar highlighting their occurrences (similar to the orange horizontal ticks in http://blogs.mathworks.com/images/desktop/michael_katz_eml/eml_editor.png ). Re (3), does it replace strings only in variable names (as intended), or in strings and comments as well? – Gnubie May 03 '12 at 11:01
  • Did you made progress with answering this question? As stated it seems unanswerable unless someone is authoritative enough to say that what you want simply cannot be done in Matlab. I agree with other posters who suggest you either clean your code (it takes too much time is a poor excuse, especially when your alternative is using my time) or find an editor more suitable to your needs. – Richard Dec 05 '12 at 15:08
  • To put that another way, your question and comments define so narrow a scope as to prevent answers from suggesting alternative solutions. Since people tend not to deal in absolutes (i.e. "it can't be done"), it is unlikely that this question will find an answer which is acceptable to you. – Richard Dec 05 '12 at 15:17
  • 1
    @Richard: Thanks for your comments. I haven't found a way to word wrap in MATLAB's editor. Though It's true my question has a narrow scope, I thought that's recommended in Stack Exchange. I think it fair to ask a question with the answer "It can't be done." I can't speak for others but I won't downvote the answer if it is later shown that it can. I'm sorry you think my reasoning is poor excuse, but whatever my reasons are is out of the scope of the question. Whether, for example, I need an answer to save lives or merely asking out of curiosity does not make the question any more or less valid. – Gnubie Dec 06 '12 at 19:00

2 Answers2

2

The way to do this is by using the three dots ... For example:

Hc_frame_1 = uicontrol(gcf, ...
'Style', 'frame',...
'Units', 'pixels',...
'Position', [25 250 200 230]);

These three dots tell MATLAB to continue the command on the next line. Without them MATLAB will think that the command is incomplete.

  • 4
    Sorry, I don't want to split a logical line physically into different parts. I want to keep it as one line in the script, but just display it in the editor wrapped, similar to Format > Word Wrap in Windows Notepad. – Gnubie May 01 '12 at 19:23
  • @Gnubie - have you stopped to consider why you need this functionality in the first place? If you've got consistently long lines that need wrapping, you should probably be refactoring a little to make those lines less long. – Dang Khoa May 02 '12 at 23:36
  • 10
    (1) I've inherited many long scripts, so it's too much trouble fixing long lines. (2) It lets me quickly comment out and uncomment lines using Ctrl-R and Ctrl-T, respectively, during debugging. With blocks, one must select the entire block, making sure no more or fewer lines are selected. – Gnubie May 03 '12 at 11:20
  • 1
    I want this functionality too, if for no other reason that sometimes I have limited screen space and would like to be able to read code in, say, a 40-char-wide window. hard-wrapping via `...` is total bogosity. – Carl Witthoft May 21 '15 at 18:04
1

There is no way to wrap general text in the editor.

It can be set to do so but just for comments.

cladelpino
  • 337
  • 3
  • 14
  • 1
    Very old related: https://superuser.com/questions/327497/can-i-make-the-matlab-text-editor-wrap-text-at-a-given-column-width-without-impl – cladelpino Sep 09 '19 at 19:55