Subplot disappeared after getting its handle

4 visualizzazioni (ultimi 30 giorni)
I have a figure consisting of five subplots and I want to save or open one of them in another figure. For some reason, whenever I tried to get the handle with the first line. That subplot specifically just turned empty and the new figure contains only an empty axis too. Any help on selecting a subplot would be appreciated.
h_subplot = subplot(5,1,2);
figure;
h_newaxis = copyobj(h_subplot, gcf);
set(h_newaxis, 'Position', get(0,'DefaultAxesPosition'));

Risposta accettata

Constantino Carlos Reyes-Aldasoro
Ok, I have now seen that the error is in the object that you are trying to copy, it should NOT be the subplot, but the actual object that you display in that subplot. Try the following:
figure
h1_subplot = subplot(5,1,2);h1_ax = imagesc(rand(16));
figure
h2_subplot = subplot(5,1,4);
h2_newaxis = copyobj(h1_ax,h2_subplot );
Hope that now the problem should be solved.
  3 Commenti
Constantino Carlos Reyes-Aldasoro
In that case, you will need to find it through handles and children. For example, take this code:
figure
subplot(3,2,1); plot(1:10,1:10,'bo');
subplot(3,2,2); plot(1:20,1:20,'rx');
subplot(3,2,3); plot(1:30,1:30,'md');
subplot(3,2,4); plot(1:40,1:40,'ys');
subplot(3,2,5); plot(1:50,1:50,'gd');
Now, we use gcf to get the figure and store this in the handle h0
h0=gcf;
get(h0)
Alphamap: [0 0.0159 0.0317 0.0476 0.0635 0.0794 0.0952 0.1111 0.1270 0.1429 0.1587 0.1746 0.1905 0.2063 0.2222 0.2381 0.2540 0.2698 0.2857 0.3016 0.3175 0.3333 0.3492 0.3651 … ] BeingDeleted: off BusyAction: 'queue' ButtonDownFcn: '' Children: [5×1 Axes] Clipping: on CloseRequestFcn: 'closereq' Color: [1 1 1] Colormap: [256×3 double] ContextMenu: [0×0 GraphicsPlaceholder] CreateFcn: '' CurrentAxes: [1×1 Axes] CurrentCharacter: '' CurrentObject: [0×0 GraphicsPlaceholder] CurrentPoint: [0 0] DeleteFcn: '' DockControls: on FileName: '' GraphicsSmoothing: on HandleVisibility: 'on' Icon: '' InnerPosition: [671 558 577 433] IntegerHandle: on Interruptible: on InvertHardcopy: on KeyPressFcn: '' KeyReleaseFcn: '' MenuBar: 'none' Name: '' NextPlot: 'add' Number: 1 NumberTitle: on OuterPosition: [671 554 577 458] PaperOrientation: 'portrait' PaperPosition: [1.3359 3.3131 5.8283 4.3737] PaperPositionMode: 'auto' PaperSize: [8.5000 11] PaperType: 'usletter' PaperUnits: 'inches' Parent: [1×1 Root] Pointer: 'arrow' PointerShapeCData: [16×16 double] PointerShapeHotSpot: [1 1] Position: [671 558 577 433] Renderer: 'opengl' RendererMode: 'auto' Resize: on Scrollable: off SelectionType: 'normal' SizeChangedFcn: '' Tag: '' ToolBar: 'none' Type: 'figure' Units: 'pixels' UserData: [] Visible: off WindowButtonDownFcn: '' WindowButtonMotionFcn: '' WindowButtonUpFcn: '' WindowKeyPressFcn: '' WindowKeyReleaseFcn: '' WindowScrollWheelFcn: '' WindowState: 'normal' WindowStyle: 'normal' XDisplay: ':1.0'
As you can see there are 5 children in the handle, you can then access these
h1 = h0.Children(2);
get(h1)
ALim: [0 1] ALimMode: 'auto' AlphaScale: 'linear' Alphamap: [0 0.0159 0.0317 0.0476 0.0635 0.0794 0.0952 0.1111 0.1270 0.1429 0.1587 0.1746 0.1905 0.2063 0.2222 0.2381 0.2540 0.2698 0.2857 0.3016 0.3175 0.3333 0.3492 0.3651 … ] AmbientLightColor: [1 1 1] BeingDeleted: off Box: on BoxStyle: 'back' BusyAction: 'queue' ButtonDownFcn: '' CLim: [0 1] CLimMode: 'auto' CameraPosition: [20 20 17.3205] CameraPositionMode: 'auto' CameraTarget: [20 20 0] CameraTargetMode: 'auto' CameraUpVector: [0 1 0] CameraUpVectorMode: 'auto' CameraViewAngle: 6.6086 CameraViewAngleMode: 'auto' Children: [1×1 Line] Clipping: on ClippingStyle: '3dbox' Color: [1 1 1] ColorOrder: [7×3 double] ColorOrderIndex: 2 ColorScale: 'linear' Colormap: [256×3 double] ContextMenu: [0×0 GraphicsPlaceholder] CreateFcn: '' CurrentPoint: [2×3 double] DataAspectRatio: [20 20 1] DataAspectRatioMode: 'auto' DeleteFcn: '' FontAngle: 'normal' FontName: 'Helvetica' FontSize: 8 FontSizeMode: 'auto' FontSmoothing: on FontUnits: 'points' FontWeight: 'normal' GridAlpha: 0.1500 GridAlphaMode: 'auto' GridColor: [0.1500 0.1500 0.1500] GridColorMode: 'auto' GridLineStyle: '-' GridLineWidth: 0.5000 GridLineWidthMode: 'auto' HandleVisibility: 'on' HitTest: on InnerPosition: [0.5703 0.4096 0.3347 0.2157] InteractionOptions: [1×1 matlab.graphics.interaction.interactionoptions.InteractionOptions] Interactions: [1×1 matlab.graphics.interaction.interface.DefaultAxesInteractionSet] Interruptible: on LabelFontSizeMultiplier: 1.1000 Layer: 'bottom' Layout: [0×0 matlab.ui.layout.LayoutOptions] Legend: [0×0 GraphicsPlaceholder] LineStyleCyclingMethod: 'aftercolor' LineStyleOrder: '-' LineStyleOrderIndex: 1 LineWidth: 0.5000 MinorGridAlpha: 0.2500 MinorGridAlphaMode: 'auto' MinorGridColor: [0.1000 0.1000 0.1000] MinorGridColorMode: 'auto' MinorGridLineStyle: ':' MinorGridLineWidth: 0.5000 MinorGridLineWidthMode: 'auto' NextPlot: 'replace' NextSeriesIndex: 2 OuterPosition: [0.5343 0.3722 0.4097 0.2693] Parent: [1×1 Figure] PickableParts: 'visible' PlotBoxAspectRatio: [1 0.4870 0.4870] PlotBoxAspectRatioMode: 'auto' Position: [0.5703 0.4096 0.3347 0.2157] PositionConstraint: 'innerposition' Projection: 'orthographic' Selected: off SelectionHighlight: on SortMethod: 'childorder' Subtitle: [1×1 Text] SubtitleFontWeight: 'normal' Tag: '' TickDir: 'in' TickDirMode: 'auto' TickLabelInterpreter: 'tex' TickLength: [0.0100 0.0250] TightInset: [0.0281 0.0374 0.0121 0.0162] Title: [1×1 Text] TitleFontSizeMultiplier: 1.1000 TitleFontWeight: 'bold' TitleHorizontalAlignment: 'center' Toolbar: [1×1 AxesToolbar] Type: 'axes' Units: 'normalized' UserData: [] View: [0 90] Visible: on XAxis: [1×1 NumericRuler] XAxisLocation: 'bottom' XColor: [0.1500 0.1500 0.1500] XColorMode: 'auto' XDir: 'normal' XGrid: off XLabel: [1×1 Text] XLim: [0 40] XLimMode: 'auto' XLimitMethod: 'tickaligned' XMinorGrid: off XMinorTick: off XScale: 'linear' XTick: [0 10 20 30 40] XTickLabel: {5×1 cell} XTickLabelMode: 'auto' XTickLabelRotation: 0 XTickLabelRotationMode: 'auto' XTickMode: 'auto' YAxis: [1×1 NumericRuler] YAxisLocation: 'left' YColor: [0.1500 0.1500 0.1500] YColorMode: 'auto' YDir: 'normal' YGrid: off YLabel: [1×1 Text] YLim: [0 40] YLimMode: 'auto' YLimitMethod: 'tickaligned' YMinorGrid: off YMinorTick: off YScale: 'linear' YTick: [0 20 40] YTickLabel: {3×1 cell} YTickLabelMode: 'auto' YTickLabelRotation: 0 YTickLabelRotationMode: 'auto' YTickMode: 'auto' ZAxis: [1×1 NumericRuler] ZColor: [0.1500 0.1500 0.1500] ZColorMode: 'auto' ZDir: 'normal' ZGrid: off ZLabel: [1×1 Text] ZLim: [-1 1] ZLimMode: 'auto' ZLimitMethod: 'tickaligned' ZMinorGrid: off ZMinorTick: off ZScale: 'linear' ZTick: [-1 0 1] ZTickLabel: '' ZTickLabelMode: 'auto' ZTickLabelRotation: 0 ZTickLabelRotationMode: 'auto' ZTickMode: 'auto'
You will need to play a bit to find which is the axes that you want to copy, but this will work.
If you want to know more about handles and how to create publication quality images, I can recommend my book:
Biomedical Image Analysis Recipes in MATLAB: For Life Scientists and Engineers
https://www.wiley.com/en-sg/Biomedical+Image+Analysis+Recipes+in+MATLAB%3A+For+Life+Scientists+and+Engineers-p-9781118657553
Hope this solves your problem, or at least points you in the right direction.

Accedi per commentare.

Più risposte (1)

Constantino Carlos Reyes-Aldasoro
The problem is the order in which you are passing the commands, you create a subplot in a figure, then you call for a new figure and then you use gcf. Try like this
h_fig = figure;
h_subplot = subplot(5,1,2);
h_newaxis = copyobj(h_subplot, h_fig);
set(h_newaxis, 'Position', get(0,'DefaultAxesPosition'));
  1 Commento
Shao-Yu Wang
Shao-Yu Wang il 6 Mag 2023
Unfortunately it didn't work. After the second line the original subplot ws still there and the new figure got a similar axis. However the fourth line simply overlapped another axis on top of it and there's still no plot data. The good news is the old subplot was intact.

Accedi per commentare.

Tag

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by