Functions and figures in Matlab

This is a funny one – how to make a Matlab figure do some stuff on-demand with key presses and GUI elements.

Doing stuff in the background while the main script is running is a bit tricky in Matlab, but there is one thing that comes to your rescue: figures. Not only they allow you to display stuff, but they also provide a basic set of UI widgets and can call functions on demand. Want a simple example?

a = figure;
a.KeyPressFcn = @handleKeyPress;

function handleKeyPress(src,event)
    fprintf( "Someone pressed: %s\n", event.Key )
end

Leave a Reply