How to control the pushbutton in a while loop in Matlab GUI

Hi all,
I am now writting a pushbutton callback function by using a while loop like below
while i > 0 ;
var = get(handles. pushbutton, ' value');
if var ==1
plot(x,y);
else
error('range quitting')
end;
end
However, by this way, the pushbutton will automatically be clicked and the while loop will run continously.
I would like to run the loop each time only when I click the pushbutton.
Can anyone help me to solve the problem?
Many thanks.

 採用された回答

Adam Danz
Adam Danz 2021 年 8 月 17 日

1 投票

> I would like to run the loop each time only when I click the pushbutton.
I don't know whether you're using app designer, GUIDE, or a custom GUI/App which prevents me from getting too specific. I'm not sure what this means: " the pushbutton will automatically be clicked". Also, where is this while-loop and what is i? It's not clear why a while-loop is needed.
Here the general steps you can take.
  1. If the button should produce a figure, assign a callback function to the button and move the plotting code to the callback function. The code will be evoked when the user pressed the button.
  2. If the button should start/stop a longer plotting process such as an animation or updating a plot according to incoming data, you could use a state button (on/off states) that determines when the plotting should happen or pause. Keep in mind that when a callback function completes, its variables are cleared so you'll have to store the progress somewhere so the plotting function can pick up where it left off next time it's evoked.

7 件のコメント

Alan
Alan 2021 年 8 月 17 日
Thanks for your answers. I want to draw multiple straight lines on an image. I supposed that if I click the push button, then I can draw a line on the image, and so on. That's why I wirte it in a while loop function in order to continously draw a line on the image. If I want to quit and stop draw the line, I can realize it without click the pushbutton.
Adam Danz
Adam Danz 2021 年 8 月 17 日
編集済み: Adam Danz 2021 年 8 月 17 日
By 'continuously draw a line' does that mean you want to animate the line being drawn or does that mean you want to continually add lines? Does each button press add 1 line? I can give more advice if I could better understand what the button is doing, exactly.
For example, does 1 button press add 1 new line or does 1 button press start the process of adding lines and another button press stops adding lines? Also, how are the lines defined? Do you have a set of coordinates that are always available and you're just indexing them or is the data developed in real time?
Alan
Alan 2021 年 8 月 17 日
Thanks for your help and answers. One button press will add 1 new line. These lines are defined by ginput function. And 1 button press, I can draw a new line. Without press the button, no line can be drawn. The process like below, you can see these lines are drawn on the image. The data of these lines are developed in real time. 1 button press, I can get the real time data of the new line.
Adam Danz
Adam Danz 2021 年 8 月 18 日
Since ginput waits for the user to interact with the figure, why do you need a while-loop? From what I understand of the description, it sounds like you just need to call ginput from the button callback function and plot the line within the callback function.
Alan
Alan 2021 年 8 月 18 日
Sorry I did not clear state the problem. I use ginput function to draw multiple lines on the image. And then I use a pushbutton to control to delete the line one by one that I do not want to keep. For example, 1 pushbutton press, one line will be removed. But now in the while loop, all the lines will be removed once I clicked the pushbutton.
My idea is that the line is removed only when I click the pushbutton. That is to say, 1 press button, 1 line is removed. This process looks like the undo function in some softwares.
Adam Danz
Adam Danz 2021 年 8 月 18 日
I have no idea how the lines are created but their handles need to be stored somehwere within the app. Since I don't know whether you're using app designer or something else, I can't suggest a specific method (use a private property for app designer, use guidata with GUIDE, use setappdata otherwise).
If the button merely deletes 1 line every time it's pressed, you'll just access the list of handles and delete the first or last handle depending on whether you want to delete the most recent or earliest lines.
I really can't give any more specific advice without knowing a lot more about what the actual goal is.
Alan
Alan 2021 年 8 月 18 日
Thanks for your suggestions and help. Some of your advices are useful to solve the probelm. Thanks a lot.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

質問済み:

2021 年 8 月 17 日

編集済み:

2021 年 8 月 18 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by