Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Continuous Output in Matlab

I have a script that is something like this:

i = 1;
while i <10000
   a = input('enter a ');
   c(i) = a;
   i = i + 1;

end

I am trying to have 'a' be saved in 'c' about every second regardless of how long the user takes to input the value or anything else going on in the loop. So for example, lets say the user inputs 3 for 'a' waits 2 secs, then inputs 6 for 'a' then waits 3 secs then inputs 12 then nothing for a while, 'c' would look like this:

c = 3 3 6 6 6 12 12 12 12 12...

Right now, 'c' looks like this:

c = 3 6 12...

which is NOT what I want. Any suggestions? it doesn't have to be a second on the dot but i want continuous output.

like image 873
user3712173 Avatar asked Dec 04 '25 04:12

user3712173


1 Answers

Your question is interesting, but is not very well specified. I'm assuming the following:

  • Each input should be immediately appended to c, and repeatedly appended again every second until a new value is entered, which resets the time count. It's not clear from your question if you want that initial, fixed "commit" of the new input to c or not.
  • You want the updated c to be automatically displayed, according to your comment to a now deleted question. You should have stated that in your question to begin with.

Then, you can use a timer object that is stopped and restarted when each new input value has been entered. The timer is configured to wake up every second. When it wakes, it appends the latest input value a to vector c and displays it. Care should be taken to stop and delete the timer when no longer needed. In addition,

  • I'm considering empty input as an exit signal; that is, empty input indicates that the user wants to finish even if the iterations have not been exhausted. Using Ctrl-C to abort input is not viable, because the timer would keep running. I don't know anmy way to intercept Ctrl-C.

  • I'm removing the prompt string from the input function input as it interferes with the automatic display of the updated c vector.

  • User input blocks execution of the program. If you want other operations done with c as it's being updated by the timer, include them in its 'TimerFcn' function. Currently that function is just 'c = [c a]; disp(c)' (append the input and display it).

Code

c = []; % initiallize
t = timer('ExecutionMode', 'fixedRate', ... % Work periodically
    'Period', 1, ... % Every 1 second...
    'TimerFcn', 'c = [c a]; disp(c)', ... % ... append latest value to c
    'ErrorFcn', 'delete(t)'); % If user ends with Ctrl-C, delete the timer
i = 1;
done = false;
clc % clear screen
while i < 10 & ~done
    a = input(''); % Input new value. No prompt string
    stop(t) % stop appending previous a
    if isempty(a)
        done = true; % no more iterations will not be run
    else
        start(t) % start timer: appends current a, and repeats every second
    end
    i = i + 1;
end
delete(t) % stop and delete timer
clear t % remove timer from workspace

Example

Here's a gif with an example run, where I'm inputting values 10, 20, 30, 40 with different pause times, and exiting with an empty input.

enter image description here

like image 113
Luis Mendo Avatar answered Dec 05 '25 20:12

Luis Mendo