More efficient way to insert a push button in a figure and run a function or a command.
Ältere Kommentare anzeigen
Hi, I wrote the following function to plot x and y and when you press a button it calls the ginput of 2 coordinates and inserts the text of the result on the plot. The problem is that I had to create the ButtonDownFcn as an entire string to be evaluated. Ok, like that is working, but how can I be more efficient and call a function in a function to do that? Thank you very much and best regards Rafael
x=1:0.01:5;
y=sin(x);
function [ ] = Plot_with_ginput_buttonfn( x,y )
figure
plot(x,y)
% create a button to calculate the difference between 2 points
h = uicontrol('Position',[5 5 150 30],'String','Calculate xdiff',...
'Callback','uiresume(gcbf)');
h.Enable = 'Inactive';
h.ButtonDownFcn=['[ppm, intensity]=ginput(2);' ...
'J=abs(diff(ppm))*600;'...
'Jstr=sprintf(''J=%.1fHz'', J);' ...
'meanppm=abs(mean(ppm));' ...
'ppmstr=sprintf(''%.3f'', meanppm);'...
'text(meanppm, mean(intensity), {[''\delta'' ppmstr]; Jstr});'];
end
7 Kommentare
Adam
am 16 Okt. 2018
I'm not entirely sure I understand what you are asking or why you cannot just set your function on the 'Callback'. I've never used a ButtonDownFcn on a pushbutton as it sounds very counter-intuitive so I wouldn't actually know which gets called first either between it and the Callback
I've also never seen a function defined in that way. It would be a lot simpler to just use a function handle and define the function in a more regular manner. You can test it more easily that way too.
Stephen23
am 16 Okt. 2018
Note that the MATLAB documentation for callback functions states clearly: "Defining a callback as a character vector is not recommended."
Evaluating strings like that is slow, buggy, and complex. You should use a function handle, just as the MATLAB documentation recommends.
"how can I be more efficient and call a function in a function to do that?"
Rafael Freire
am 16 Okt. 2018
Rafael Freire
am 16 Okt. 2018
Rafael Freire
am 16 Okt. 2018
Rafael Freire
am 16 Okt. 2018
Bearbeitet: Rafael Freire
am 16 Okt. 2018
Steven Lord
am 16 Okt. 2018
"Graphics callback functions must accept at least two input arguments:
- The handle of the object whose callback is executing. Use this handle within your callback function to refer to the callback object.
- The event data structure, which can be empty for some callbacks or contain specific information that is described in the property description for that object."
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Entering Commands finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!