More efficient way to insert a push button in a figure and run a function or a command.
조회 수: 34 (최근 30일)
이전 댓글 표시
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
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."
채택된 답변
Jan
2018년 10월 16일
편집: Jan
2018년 10월 17일
What about using the Callback instead of the ButtonDownFcn?
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', @JCal);
function JCal(ButtonH, EventData)
[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
end
댓글 수: 4
Adam
2018년 10월 17일
편집: Adam
2018년 10월 17일
Callbacks must have 2 input arguments (as per the link Steven Lord shared above), which I usually call src and evt (but you can call them what you want, as you see). They represent the source object (the control that triggered the callback) and the event data which is usually useless, but for a small number of components carries useful information (e.g. tab group tab selected event tells you which tab you came from and which you are going to).
After that Matlab doesn't care what arguments you include except for the usual function rules that if you refer to something in the function that hasn't been declared or passed in as an argument it will error.
You can define any function with 15 arguments and use none of them in the function body if you really wish though!
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!