MATLAB return current values on interrupt [duplicate] - matlab

This question already has answers here:
In Matlab, is it possible to terminate a script, but save all its internal variables to workspace?
(3 answers)
Closed 7 years ago.
If I press Ctrl + C during the execution of a long loop, I lose all the work that has done so far. So is there a way in MATLAB that I can press Ctrl + C, but return the current variables?
so what I want as a pseudo-code :
for i=1:very_long
do_things();
if keypress(ctrlc_orsomeothercombo)
disp('Im bored!');
return;
end
end
Is this possible?
Thanks

If you turn stop on errors on, then interrupting it, even with ctrl+c, brings you to the place where it was executing and you have the whole workspace available:
dbstop if error

Related

I want command + f to always search file in vscode regardless of focus [duplicate]

This question already has answers here:
How do I shut off the new Explorer Ctrl+F Find tool in VSC?
(2 answers)
Closed 4 months ago.
I do not want this green box to appear when pressing command + f in vscode and the sidebar is focused. i want command + f to always enable search within the file. this green box is useless to me because its functionality already provided by the superior command palette (command + p)
"workbench.list.defaultFindMode" seems to be the control for it, but valid values for this are only 'filter' and 'highlight', no way to get rid of it.
Version: 1.70.2
help?
Ah, finally found it.
remove the command + f keybinding from list.find

How to get multiple cursors in VS Code at beginning or end of lines? [duplicate]

This question already has answers here:
Multiline editing in Visual Studio Code
(34 answers)
Closed 2 years ago.
In Atom, you are able to select multiple lines then hit Command + Shift + L followed by the left arrow key or the right arrow key to put multiple cursors at the beginning or end of each selected line.
When trying to do the same command in VS Code it doesn't work? How can I achieve the same behavior in VS Code?
Shift+Alt+I will put cursors on each line end.
For the beginnings, I guess you can just Ctrl+Alt+Down at the beginning to add more cursors.

Stopping/pausing execution in Matlab to check the value of the variables while running [duplicate]

This question already has answers here:
Stop and continue execution from debugger possible?
(6 answers)
Closed 6 years ago.
I am trying to write a program in Matlab which is quite large and I want to stop or pause the execution to see what my variables values are.
I want to stop or pause the execution to see what my variables are.
One alternative is to use keyboard:
keyboard pauses execution of a running program and gives control to
the keyboard. Place the keyboard function in a program at the location
where you want MATLAB® to pause. When the program pauses, the prompt
in the Command Window changes to K>>, indicating that MATLAB is in
debug mode. You then can view or change the values of variables to see
if the new values produce expected results. The keyboard function is
useful for debugging your functions.
To continue executing your function, type dbcont, or type dbquit to quit the debug mode.
If you want to view the variables after a given number of iterations you can insert if ii = stop_point; keyboard; where ii is the iterator of the loop.
Another option is:
Use dbstop. This is shown with an example from the documentation:
Set a breakpoint to stop when n >= 4, and run the code.
dbstop in myprogram at 4 if n>=4
myprogram
Yet another option is to manually insert breakpoints in the MATLAB editor. Click on the left side of the code to insert breakpoints (indicated by a red dot). Now you can view the variable by hovering the mouse over the variable names in the editor
Have a look at this very relevant link to get more information.
Note, the following paragraph is a direct copy of chappjc's answer here. Please upvote his/her answer if you like this approach!
With the release of R2016a, you can just hit the Pause button in the code editor and it will halt right away.
To pause the execution of a program while it is running, in the Editor
tab, click the Pause button. MATLAB pauses execution at the next
executable line*. When your code is running, the Start button will
turn into a pause:

Terminating input using Ctrl-D character not working [duplicate]

This question already has answers here:
Ctrl+D doesn't stop application from executing in command window
(5 answers)
Closed 7 years ago.
I attempted to write a Perl script to take user input. I am
on Windows 8 and Cygwin.
When I try to run my code it expects user input as it should, but when I attempt to press Ctrl-D the program still tries to accept input instead of signalling the end of input.
Here is my code below. Why is this the case? Is there a way I can overcome this without switching away from Cygwin? I do not want to press Ctrl-Z or Ctrl-C as I don't want to stop the execution of the program.
#!/usr/bin/perl
while ( $line = <> ) {
print $line;
}
In Windows you have to use CTRL-Z instead of CTRL-D.

How to stop a Matlab script but don’t kill the process? [duplicate]

This question already has answers here:
Stop and continue execution from debugger possible?
(6 answers)
Closed 8 years ago.
Strg+C stops and kills a Matlab script (at least sometimes). But is there a way to stop a Matlab, take a look at some variables and continue the calculation?
I am not talking about just setting a breakpoint. I want my script, let’s say run for couple hours come back to it hit some buttons that stops the calculations take a look at some variable and then continue the calculation.
I tried to find out if there is some shortcut key for this – I am quite sure there isn’t.
Now I was thinking about including an if-case that looks if a certain button was pressed by the user. If so there would be a useless k=0 line and a breakpoint on it. And if no one is pressing this button the loop would continue. But this is where my limited Matlab knowledge leaves me. I don’t know if there is a way to ask for a user-button press but don’t wait for a button press like in the function input. Also I just have a running script, I don’t have any GUI.
To drop to the command prompt you need the command keyboard and then type return when you have finished (you don't need a breakpoint). The tricky bit is how to trigger it. There a few options. The easiest is to open a figure window. The following code halts the process when any key is pressed.
keyDownListener=#(src,event) keyboard;
fig = figure;
drawnow
set(fig,'KeyPressFcn',keyDownListener)
for p=1:10000
%do some thing
end
You can modify this to test for a specific key since the keypress is contained within the event struct.
To use no figure gui at all its more of a problem. I'm not aware of a non blocking keyboard input method. A mex file the runs kbhit() in C might do it, but kbhit() is not standard C so it would only work on Windows. An easier option maybe to test for the presence of a file.
for p=1:100000
if exist(fullfile(pwd,'halt.tmp'),'file')
keyboard
end
%do something here
end
This drops to the debug console when halt.tmp is created in the current directory.
Other potential methods could involve using multiple threads to read 'input' (either the Parallel computer toolbox or undocumented Java code), or using http://psychtoolbox.org/ as mentioned by #bdecaf