Matlab

pause function in MatLab

The following article explains how to use the pause() function in MATLAB®, its main features, and the various control options. You will also find several practical examples of creating console applications with pictures to help you understand the pause() function in MATLAB.

MATLAB pause() function Syntax

pause(s)
pause(state)

MATLAB pause() Description and examples

The pause() function causes a temporary pause in the program’s execution. The duration of this pause is controlled by “s”, and the time unit is expressed in seconds. This function accepts fractional numbers for pauses with time intervals of less than one second. The user can interrupt this pause process before the preset time has elapsed. To do this, you must press the key combination Ctrl+c. The pause generated by this function can be enabled or disabled via the “on/off” flags depending on the programmer’s requirements. The state of these flags can also be queried using the ‘query’ option if required. The input arguments to this function are non-negative and real numbers. The data types accepted by a pause() are single, double, int8, int16, int32, int64, uint8, uint16, uint32, and uint64.

How to generate a pause in the program’s execution with the pause() function of MATLAB.

In this example, we will see how to create a pause in program execution. To do this, we will create a simple console application in which we use the pause() function to create it, the prompt() function to enter the pause time we choose via the command console and the disp() function to show us the start and end of the pause and the seconds of duration we choose. The prompt() function stores the seconds entered by the user in the command console in “s” and then sends this variable as an input argument to the pause() function. This sequence is repeated in an infinite loop.

Create a script, paste this code and run the script. To close the application, press Ctrl+c.

pause('on')

while 1

prompt = 'Enter the time expressed in seconds and press enter';

s=input(prompt);

disp ( ['The ', num2str(s),' seconds pause start'])

pause (s)

disp ( ['The ', num2str(s),' second pause is over'])

end

How to enable, disable and query the status in the MATLAB pause() function.

The pause() function can be enabled or disabled depending on the programme’s requirements. This is done via the on/off flags, and its status can be queried through the “query” option.

In this example, we will create a console application to use these three options. We can change directly via strings and numeric variables that we insert into the input arguments of the pause() function via the command console.

Since this application is an infinite loop, we will tell the user how to get out of this loop. Using the prompt() function, we will send the following message:

“To exit the infinite loop, press Ctrl+c, press Enter to continue… “

After that, the application will enter an infinite loop, which is the following sequence:

1. We retrieve the state of the pause() function with the “query” option and store the result in “q”.

2. Using the prompt() function, we inform the user of this status with the following message “The status of the pause is: (status)”. Select type “on” to enable or “off” to disable”. The user can enable or disable the pause() function by entering one of the on/off options.

3. The response entered by the user is compared with the strings “on” and “off”. If it matches one of these strings, it is sent as an input argument to the pause() function to enable or disable it.
4. The status of the function is queried again, and if the result is “on”, the user is prompted to enter the time in seconds.

5. The response entered by the user is sent as an input argument to the pause() function, and the following message is displayed: “The (time) seconds pause begins”, and after the time has elapsed, the following message “The (time) seconds pause is over”.

6. The loop returns to the beginning.

Create a script, paste this code and run the script. To close the application, press Ctrl+c.

prompt ='To exit the infinite loop press Ctrl+c. Enter to continue... ';

a=input(prompt);

while 1

q= pause('query');

prompt = ( ['The status of pause() is: "', num2str(q),'". Select type "on" to enable or "off" to disable']);

str = input(prompt, 's');

if str == "off" || str == "on"

pause(str)

end

if pause('query')=="on"

prompt = 'Enter the pause time and press Enter ';

s = input(prompt);

disp ( ['The ', num2str(s),' seconds pause start'])

pause(s)

disp ( ['The ', num2str(s),' second pause is over'])

pause(3)

end

end

How to use the “oldState” option in the MATLAB pause() function

The pause() function has an “oldState” option used to save the current execution state before changing it. In the following example, we will see how to use this option. We will create a console application where we enable and disable the pause function using the “on/off” flags and display the current state and the previous state in “oldState”.

Create a script, paste this code and run the script. To close the application, press Ctrl+c.

oldState = pause('query');

while 1

st= pause('query');

disp( ['The current state of this function is: "',num2str(st), '" and oldState is: ', num2str(oldState)])

prompt = 'To enable the function press 1, to disable press 2';

a=input(prompt);

if a==1

oldState = pause('on');

else a==2;

oldState = pause('off');

end

end

Conclusion:

This article has explained the pause() function in MATLAB and includes some practical examples that show how to use it. The individual input arguments and supported data types have also been explained. We have created console applications to demonstrate this feature’s various options in real-time execution. We hope you have found this MATLAB article useful. Thank you for reading our topics. Check out other Linux Hint articles for more tips and information.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).