MATLAB : update plot with a pushbutton

10 visualizzazioni (ultimi 30 giorni)
Sarah Guiffray
Sarah Guiffray il 20 Mar 2015
Modificato: Adam il 20 Mar 2015
Hello,
I have a figure with a graph and 2 different plot on this graph. I add an edit text with a push button in my figure near my graph.
Button_app=uicontrol('Parent',fig,'Style','pushbutton','String','Ok','Units','normalized','Position',[0.82 0.4 0.1 0.03]);
edit=uicontrol('Parent',fig,'Style','edit','String','','Units','normalized','Position',[0.82 0.45 0.1 0.03]);
And I would like the user write a number in the edit text and update one of my plot (by adding this number in each value of this plot). But I don't know how to do it ? I have a function callback when I push the button but I don't know how to get the value when the user write in the edit text.
set(Button_app,'Callback',@Callback)
Thank you in advance,
Best regards

Risposta accettata

Adam
Adam il 20 Mar 2015
If you simply want to know how to get a value from an editbox then:
newVal = str2double( edit.String );
(Don't call your varaible 'edit' though - this is a builtin function and even if it is not a function you intend to use just don't get into the habit of overwriting function-names with variables).
As far as updating your graph goes, the user just enters a single number? So is this just expected to be appended as a y value and an x value created just as the next increment of x?
One way to do this fairly neatly is to keep the handle of your original plot e.g.
hLine = plot(...);
Then edit the 'XData' and 'YData' properties with the new data e.g.
xData = get( hLine, 'XData' );
yData = get( hLine, 'YData' );
set( hLine, 'YData', [yData newVal], 'XData', [xData xData(end) + 1] );
Unless you have a large amount of data you could probably just delete the graph and replot the whole thing too, but I prefer to just edit the existing line object in such cases.
  2 Commenti
Sarah Guiffray
Sarah Guiffray il 20 Mar 2015
it does not work. I want to put a the newVal in the parameters of my callback function of the push button but the value is always empty ...
set(Button_app,'Callback',{@OffsetCallback,str2double(edit_offset.String)})
Adam
Adam il 20 Mar 2015
Modificato: Adam il 20 Mar 2015
Just pass 'edit_offset' to your callback instead.
The arguments to a callback are evaluated at the point the callback is set. That means the code
edit_offset.String
will be evaluated when you set the button callback. I'm guessing that at that point your edit box is empty. Either way, it will never update when your edit box changes content. If you pass just the editbox handle then you can extract the string from it within the callback. The editbox handle will remain constant as it should, but the string will be whatever is currently in the editbox when the callback is executed if you do it that way.

Accedi per commentare.

Più risposte (1)

Image Analyst
Image Analyst il 20 Mar 2015
It's tedious and confusing, as you've found out, to handle all those kinds of complicated details to build a GUI yourself and get them all correct. Why not use GUIDE or MAGIC?

Categorie

Scopri di più su Graphics Object Identification in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by