More efficient way to insert a push button in a figure and run a function or a command.
显示 更早的评论
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 个评论
Adam
2018-10-16
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
2018-10-16
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
2018-10-16
Rafael Freire
2018-10-16
Rafael Freire
2018-10-16
Rafael Freire
2018-10-16
编辑:Rafael Freire
2018-10-16
Steven Lord
2018-10-16
"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."
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!