force stop condition in a loop

can i add a condition in a loop that identify force stop (ctr+c) ? for example i control with matlab in a current source in a infinite loop (while(1)), and i want it to turn off when i stoped the loop with ctr+c for example: if force stop fprintf(device, ':OUTP OFF'); end

Antworten (2)

Dennis
Dennis am 30 Jul. 2018
Bearbeitet: Dennis am 30 Jul. 2018

1 Stimme

You can use onCleanup.Minimal exampel:
mytest()
function mytest()
finishup = onCleanup(@() mycleanup());
myloop();
end
function myloop()
while true
disp('running')
pause(1)
end
end
function mycleanup()
disp('stopped')
end
However maybe a loop that runs until a specific button is pressed is a cleaner approach.
KSSV
KSSV am 30 Jul. 2018

0 Stimmen

HAve a look on break.

3 Kommentare

liran avraham
liran avraham am 30 Jul. 2018
Bearbeitet: liran avraham am 30 Jul. 2018
i know break, how it can help me? i want a way the code to identify force break and to do something when it happens
KSSV
KSSV am 30 Jul. 2018
Whats the criteria for force stop?
liran avraham
liran avraham am 30 Jul. 2018
the loop is some homemade temperature control, that why the loop is infinite, so i force break when i want to change the temperature or to stop measuring.

Melden Sie sich an, um zu kommentieren.

Produkte

Version

R2015b

Gefragt:

am 30 Jul. 2018

Bearbeitet:

am 30 Jul. 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by