필터 지우기
필터 지우기

Changing default figure toolbar button's callback in App designer

조회 수: 9 (최근 30일)
Petri
Petri 2019년 6월 6일
답변: Aaron Ward 2021년 1월 19일
So I have made a simple app with the App Designer and have run into some issues. I have a figure and some buttons on the app. Issue is that when I pan the figure it does not reset the axes properly, hence, there are white borders all around my figure.
I can manually fix this by triggering app.UIAxes.XLim and YLim with values taken from the size of the actual image in the figure. The issue is how do I trigger this.
There is already a "Reset View" button in the figure but does not work. It resets the figure to some values prior to me manually fixing the XLimand YLim so there are still white borders around my image. Appanretly there is an internal function resetplotview which could be used, but it does not seem to work.
I could make one extra button to my control panel which then does my own "reset", but I was thinking that there is already a "Reset View" button in the figure itself so maybe I can just change the callback function? Well I found the button by findall(app.UIAxes.Toolbar,'Tooltip','Restore View'); but if I change the ButtonPushedFcn to my own function it just gives me this error:
Warning: Undefined function 'restoreview' for input arguments of type
'matlab.ui.controls.ToolbarPushButton'.
Error while evaluating ButtonPushedFcn for axes toolbar.
I have the restoreview function in my script file:
function restoreview(e,d)
% Default ButtonPushedFcn for the reset view toolbar button
@(e,d)matlab.graphics.controls.internal.resetHelper(d.Axes,true);
% Add our own end, which is to reset the axes manually
app.UIAxes.XLim=[0, im.XData(2)];
app.UIAxes.YLim=[0, im.YData(2)];
end
Any help?
I know I can use just a character vector as a callback and it'll execute it as command(s) but that's realllly not what I want to do.
  댓글 수: 2
Bas Peters
Bas Peters 2019년 7월 10일
Did you already find a solution? I have the same problem.
Petri
Petri 2019년 11월 4일
편집: Petri 2019년 11월 4일
EDIT: IT WORKS?!
So I decided to try the way I initially did not get to work and apparently it works.
restoreviewbtn.ButtonPushedFcn=...
{@restoreview,app,im}; % Change the callback function
% New callback function for restore view
function restoreview(e,d,app,im)
% Default ButtonPushedFcn for the reset view toolbar button
@(e,d)matlab.graphics.controls.internal.resetHelper(d.Axes,true);
% Add our own end, which is to reset the axes manually
app.UIAxes.XLim=[0, im.XData(2)];
app.UIAxes.YLim=[0, im.YData(2)];
end
No clue whether this version would've worked when I was wrinting this question but at least it works in r2019b.
OLD VERSION:
Oh well this was an old one but in case someone else finds this problematic I found an ugly way around this.
restoreviewbtn.ButtonPushedFcn=...
'app.UIAxes.XLim=[0, im.XData(2)]; app.UIAxes.YLim=[0, im.YData(2)];';
Honestly, I don't remember what this exactly does but I assume it literally makes the function to that piece of string there.

댓글을 달려면 로그인하십시오.

답변 (1개)

Aaron Ward
Aaron Ward 2021년 1월 19일
I recently worked out another solution. Note this applies to R2020A (I haven't tested other versions). The steps are as follows:
  1. Create the toolbar without the 'restoreview' button.
  2. Create the 'restoreview' button via 'axtoolbarbtn' function.
  3. Create a 'ButtonPushedFcn' for the newly created button and supply the callback handle.
  4. In the callback function, reset the axis limits.
% step 1
app.tb = axtoolbar(app.axis, {'zoomin', 'zoomout', 'export'});
% step 2
btn = axtoolbarbtn(app.sagittal_axes_tb, 'push');
btn.Icon = 'restoreview';
% step 3
btn.ButtonPushedFcn = createCallbackFcn(app, @restoreview, true);
% step 4
function restoreview(app, event)
limits = [lower upper];
app.axis.XLim = limits;
app.axis.YLim = limits;
end

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by