0

I want to create a frontend where the user can browse pictures forward by pressing Enter. Pseudo-Code

hFig=figure
nFrames=5;
k=1;
while k < nFrames
   u=signal(1*k,100*k,'data.wav'); % 100 length

   subplot(2,2,1);
   plot(u);

   subplot(2,2,2);
   plot(sin(u));

   subplot(2,2,3);
   plot(cos(u));

   subplot(2,2,4);
   plot(tan(u));

   % not necessary but for heading of overal figure
   fprintf('Press Enter for next slice\n');
   str=sprintf('Slice %d', k);
   mtit(hFig, str); 

   k=k+1;
   keyboard

end

function u=signal(a,b,file)
   [fs,smplrt]=audioread(file);
   u=fs(a:b,1);
end

where

  • something is wrong in updating the data because pressing CMD+Enter increases k by one but does not update the data. Sometimes (rarely), the data is once the next iteration.
  • something is wrong with while's condition because k can be bigger than nFrames. keyboard just keep asking for more inputs.

My mistake earlier in Error-Checking

I had earlier a problem where the closure of the window lead to the crash of the application. I include this here because I mentioned a problem about it in the comment of one answer. I avoid the problem now by

hFig=figure;
n=5;
k=1;
while k<nFrames 

      % for the case, the user closes the window but starts new iteration
      if(not(ishandle(hFig)))
          hFig=figure;
      end

   ...

end

which creates a new Figure if the earlier was closed by the user. I tried unsuccessfully putting hFig=figure; inside the while loop's if clause earlier to avoid repetition in the code. Please, let me know if you know why you cannot have the handle hFig in the while loop's if clause.


How can you loop subplots with updated outputs in Matlab?

3
  • You need to read the documentation for functions you use. keyboard enters the debugger, the function is continued with F5 (by default). You're probably better off using something like pause. Commented Mar 2, 2016 at 19:24
  • @excaza Not sure if it is F5. Now, I am in OS X where it should be FN+F5 or CMD+Enter but I am having a difficulty in detecting if the input was accepted. Commented Mar 2, 2016 at 19:49
  • The point of my comment is that you don't want to be in the debugger. Commented Mar 2, 2016 at 19:54

1 Answer 1

2

To stop the script waiting for an input from the user you should use input instead of keyboard.

Actually keyboard makes your script entering in a debug mode. It stops the executino of the script as (like a breakpoint) allowing the user to, for example, check the value of a variable.

You can modify your scripr as follows (modification are at the end of your script, identified by "UPDATED SECTION):

hFig=figure
nFrames=5;
k=1;
while k < nFrames
   u=signal(1*k,100*k,'handel.wav'); % 100 length

   subplot(2,2,1);
   plot(u);

   subplot(2,2,2);
   plot(sin(u));

   subplot(2,2,3);
   plot(cos(u));

   subplot(2,2,4);
   plot(tan(u));

   % not necessary but for heading of overal figure
   %
   % UPDATED SECTION
   %
   % Use the string "Press Enter for next slice\n" as the prompt for the
   % call to "input"
   %
   % fprintf('Press Enter for next slice\n');
   % str=sprintf('Slice %f', k);
   % Use %d instead of "%f" to print integer data
   str=sprintf('Slice %d', k);
   mtit(hFig, str); 

   k=k+1;
   % Use "input" instead of "keyboard"
   % keyboard
   input('Press Enter for next slice\n')

end

Hope this helps.

Qapla'

Sign up to request clarification or add additional context in comments.

4 Comments

Can you output something immediately if correct input given? I have a difficulty that I feel that the software goes to some loop at some stages and I do not know if the input has been accepted.
I do not understand your question: do you want the user inserts a value for k?
Sorry for the confusion. I had earlier a problem with the error checking. The closure of the window caused my application to be in infinite loop. I updated the body to show about how I fixed the issue.
Yes, working technically. There may be better ways to do the error checking. I am always scared of repetition in my code, which I do there. Other error checks may also be necessary but I need to iterate now a few times to find them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.