Spacey
Spacey

Reputation: 3011

Is there a way to 'jump scroll' in MATLAB?

In MATLAB editor, you can scroll through lines using the up/down arrow keys obviously, and you can jump across pages using the page up/down buttons.

However, is there a way for me to scroll through say, 10 lines at a time? Or some other programmable number of lines? I thought I could do this with the CTRL key, but this does not seem to work...

Thanks.

Upvotes: 1

Views: 821

Answers (3)

proteome
proteome

Reputation: 326

Here's a two-part solution that lets you choose whatever hotkey and scroll amount you want. We'll first create a function, jumpscroll, that moves the caret an arbitrary number of lines, preserving its column position:

function jumpscroll(n)
  editor = com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor;
  line_col = editor.positionToLineAndColumn( editor.getCaretPosition );
  editor.goToLine( line_col(1)+n, line_col(2) )

Matlab keyboard shortcuts don't have the flexibility to call random functions, but Yair Altman's EditorMacro mostly fixes this deficiency. The function is called with 3 arguments: a key combination, an action to perform upon hitting those keys, and the type of macro/shortcut. Check out the internal function documentation for specifics.

Run in the command window, the following lines add jumpscroll(n) to the key set (ctrl+shift+up/down) for both the arrow keys ('UP'/'DOWN') and numpad arrows (KB_UP/KB_DOWN).

n = 10; % number of lines to move up or down
EditorMacro('shift ctrl pressed UP',      {@jumpscroll,-n}, 'run');
EditorMacro('shift ctrl pressed KP_UP',   {@jumpscroll,-n}, 'run');
EditorMacro('shift ctrl pressed DOWN',    {@jumpscroll, n}, 'run');
EditorMacro('shift ctrl pressed KP_DOWN', {@jumpscroll, n}, 'run');

It's now trivial to change the number of lines skipped or the key combination. Unfortunately, the keybindings persist only for the current Matlab session and must be reloaded each new session. Ideally, the lines would go in startup.m, but that causes a problem where EditorMacro tries to access the editor handle before it's completely loaded. So... it seems necessary to manually run those lines after starting Matlab. If anyone has a better idea, please reply!

Both parts of this solution rely on some hidden guts of Matlab and may break in future versions. Yair explains EditorMacro and what it's doing quite well at UndocumentedMatlab.com. (And I found his tool uiinspect essential for understanding the editor objects enough to write jumpscroll.)


This last section is a comment on Bin Lin's answer, but I don't yet have the S.O. reputation to actually put it there.

If you're using Windows, the keyboard macro program AutoHotkey can fix the mouse problem. Among many other things, it lets you control the mouse scroll wheel with a key press. And instead of setting the mouse wheel properties to scroll by a larger amount, you can mimic n wheel clicks with a single key press. There's probably something similar for Mac/Linux.

Upvotes: 1

Bas Swinckels
Bas Swinckels

Reputation: 18488

Not that I am aware of, but one thing that comes close and that I find pretty useful is to use cell mode. The way it works is to divide up you code in logical sections with comments of the form '%% section heading' (the first space is required):

%% this is section one

some_code_here(); % a normal comment
bla;

%% this is section two

code_for_section_two_goes_here;

If you then switch on cell mode (menu bar in the editor->Cell->Enable cell mode) , the editor makes it pretty clear which section you are working in by drawing some thin gray lines and changing the background color. This by itself is not interesting, but you can have Matlab execute just the code in the current cell of a script by positioning the cursor anywhere within the cell and pressing CTRL+Enter. You can also move one cell up or down by CTRL+/ (similar to what you want) and a few more things like execute current cell and jump to the next one by CTRL+SHIFT+Enter. All of these can be customized under File->Preferences->Keyboard->Shortcuts.

Dividing your code with comments into logical sections is something you should do anyhow in well-maintained code, so this is hardly a problem. I use it all the time to divide a script into a section that loads some stuff from file (which might take a long time), followed by some calculations (which you might want to change a few times, without reloading the data every time), followed by another section to make a plot (which you might have to edit and execute many times to get right). Using cell mode, you go through your code section by section and repeat it as many times as necessary to get it right.

Upvotes: 1

Ben Lin
Ben Lin

Reputation: 835

If you are using Windows, you can use the scroll wheel settings (In Control Panel, Mouse, tab Wheel:

Roll the wheel one notch to scroll: 
   The following number of lines at a time: 10

Upvotes: 2

Related Questions