4

I have a function that I would like to take input for but only when the user wants to. For example if i have this code:

figure
amplitude = 10;
tic
i = 1;
while(1) 
    time = toc;
    values(i) = amplitude*sin(time);
    times(i) = time;
    plot(times, values)
    drawnow
    i = i+1;
end

You will get a continually plotting sine wave (like a lame movie). What I want to do is allow the user to change the amplitude of the wave at any time. That is the program will continue to run but if the user types in 20 and Enter then the amplitude variable can be changed and the sine wave will change amplitude in the movie. Any pointers on how to achieve this?

4
  • Common guys, the question is well worded and has a good code example. If you upvote it then it will gain some attention and might get answered. Commented Feb 15, 2013 at 0:35
  • 1
    You won't be able to do this by typing numbers into the console, but you can do it with a simple GUI. Do a google search for Matlab callbacks to get going Commented Feb 15, 2013 at 0:40
  • @learnvst Add it as an answer! Commented Feb 15, 2013 at 0:55
  • as you wish. There should be a simple GUI tutorial in the Matlab documentations also Commented Feb 15, 2013 at 1:02

2 Answers 2

4

You won't be able to do this by typing numbers into the console, but you can do it with a simple GUI. Do a google search for Matlab callbacks to find examples. When a GUI event occurs, it triggers a function that you can use to modify the variables in your loop.

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

Comments

1

It is probably best to do it with a GUI as mentioned, but if you just want something in the console here is what I can offer:

A script that periodically asks the user to input an amplitude and then continues the 'movie' with this amplitude. It can easily be expanded to allow the user to decide when he will be asked to input the next amplitude change.

clear
amplitude = 10;
i=1;
while(1) 
    time = i/1000;
    values(i) = amplitude*sin(time);
    times(i) = time;
    plot(times, values)
    drawnow
    i = i+1;
    if mod(i,3141) == 0
        keyboard
    end
end

Now this will run for a while and then ask you to input the next amplitude. Note that you can actually give multiple commands at once.

amplitude = 20; return
amplitude = 1; return

This will let the next amplitude be 20 and the one after that 1. Note that the upward arrow key is your friend here.

Comments

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.