Creating continuous output in Matlab

I have a script, something like this:

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

end

      

I am trying to store "a" to "c" every second no matter how long it takes for the user to enter a value or whatever happens in a loop. For example, let's say user inputs 3 for "a" wait 2 seconds, then inputs 6 for "a" then wait 3 seconds, and then inputs 12, then nothing for a while for "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 second on point, but I want continuous output.

+3


source to share


1 answer


Your question is interesting but not well stated. I am assuming the following:

  • Each input must be immediately added to c

    and re- added again every second until a new value is entered that resets the time count. It is not clear from your question whether you want the initial, fixed "commit" of the new input on c

    or not.
  • You want the updated one to c

    be automatically displayed , as per your comment on the now deleted question. You should have pointed out what in your question to begin with.

Then you can use an object that stops and restarts when each new input value has been entered. The timer is set to wake up every second. When it wakes up, it adds the last input value to the vector and displays it. Care should be taken to stop and remove the timer when it is no longer needed. Besides, timer

a

c

  • I regard an empty entry as an exit signal ; that is, empty input indicates that the user wants to complete, even if the iterations have not been exhausted. Using Ctrl- Cto cancel an input is not viable as the timer will continue to run. I do not know which way to intercept Ctrl- C.

  • I am removing the prompt string from the input function input

    as it interferes with the automatic display of the updated vector c

    .

  • User input blocks program execution . If you want to do other operations with c

    as it is updated by the timer, include them in your function 'TimerFcn'

    . This feature is currently simple 'c = [c a]; disp(c)'

    (add it and show 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

This shows the gif to the example run, where I introduced the values 10

, 20

, 30

, 40

at different times of pause and left with an empty input.

enter image description here

+1


source







All Articles