Copying the content of a Figure to another Figure (2024)

571 views (last 30 days)

Show older comments

HA on 12 Feb 2019

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure

Commented: Walter Roberson on 14 Feb 2019

Accepted Answer: Adam Danz

Open in MATLAB Online

Hi,

I am trying to copy the directivity pattern of an antenna array from the figure generated by the pattern command to another window but so far did not manage to do that.

I am using copyobj command but it does not work. Here is the code that I am executing to achieve this goal:

fc = 60.48e9; % Carrier Frequency in Hz.

numElements = 16; % Number of antenna elements.

prop = physconst('LightSpeed');

lambda = prop/fc;

% ULA Antenne Array Creation.

array = phased.ULA(numElements, lambda/2);

f1 = figure();

pattern(array, fc, -180:180, 0, 'PropagationSpeed',prop,...

'CoordinateSystem', 'polar',...

'Type', 'powerdb', 'Normalize', true);

ax1 = gca;

f2 = figure();

ax = axes;

ax2 = copyobj(ax1,ax);

I am doing that because I did not manage to find a way to solve the problem listed in this Question. Using the method above, I will copy the content of the figure to a polaraxes in UIFigure in Matlab App Designer.

Any solution to one of the mentioned problems is highly appreciated.

4 Comments

Show 2 older commentsHide 2 older comments

Star Strider on 12 Feb 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_670539

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_670539

Open in MATLAB Online

I do not have the Phased Array System Toolbox, so I cannot experiment with your code. However, it would be worthwhile to run these commands:

ax1 = gca;

gax1 = get(ax1)

to see what properties you have access to. You might be able to extract the relevant data using those properties, the findobj function, or some combination of them.

Jan on 12 Feb 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_670558

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_670558

I do not understand, what exactly "one of the above problems" are. What ist your question? What does "it does not work" mean? Please explain, what you observe.

HA on 12 Feb 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_670608

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_670608

@Jan, I think the question is clear. The code above is not doing what it should do i.e. copying the content of one figure to another.

Adam Danz on 12 Feb 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_670630

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_670630

But Jan wants to know what you mean by "it does not work". For example, are you getting an error message or a warning message? Is the image appearing but with missing objects? Does the code freeze? This type of clarity gives us a starting point to come up with ideas for a solution. Once I ran the code myself, seeing the error message immediately guided me to the solution. Since you didn't inlude an error message, Jan rightfully asked for clarity.

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Adam Danz on 12 Feb 2019

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#answer_360742

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#answer_360742

Edited: Adam Danz on 13 Feb 2019

Open in MATLAB Online

The function copyobj(h,p) copies objects listed in h to a new parent, p. When your code is run, the error message indicates, "Axes cannot be a child of Axes." This is because you cannot assign one axis as a child to another axis. Rather, you should copy the axis to a new figure.

Here's the correct way to do that:

f2 = figure();

ax2 = copyobj(ax1,f2);

[UPDATE]

To copy objects from one set of axes to another, you must first get handles to all of the axis children and then copy that list of handles to an existing axis. Here's a demo that follows the code in your question.

% Get handles for all children from ax1

ax1Chil = ax1.Children;

% Copy all ax1 objects to axis 2

copyobj(ax1Chil, ax2)

In your case, even though the axes you're copying from are polar axes, the data are actually plotted in Cartesian coordinates. So you'll copy the objects to a cartesian plot (middle figure below).

ax1Chil = ax1.Children;

figure;

ax2 = axes;

copyobj(ax1Chil, ax2)

axis equal

rectangle('Position', [-50 -50, 100, 100], 'Curvature', [1,1]) %Example: add circle

If you add the object to a polar axis, the (x,y) coordinates create a completely different pattern (right figure below).

ax1Chil = ax1.Children;

figure;

ax2 = polaraxes('ThetaTick', -150:30:180, 'ThetaLim', [-180, 180]);

copyobj(ax1Chil, ax2)

The image below shows [original, Cartesian axes, polar axes].

Copying the content of a Figure to another Figure (7)

8 Comments

Show 6 older commentsHide 6 older comments

HA on 13 Feb 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_670762

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_670762

Thanks for the answer. It solves the error but does not solve my problem in general.

I would like to copy the axes of the generated figure to the axes of a specified figure. So instead of usinmg f2 as a parent I would like to use axes but not a parent but rather as a destination.

Walter Roberson on 13 Feb 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_670764

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_670764

you cannot do that . You can copy axes children and you can extract settings of the axes and set them in the new axes. You would want to be selective about what you set as you probably do not want to set position or parent .

You should expect copying of legends and colorbar to fail .

Watch out for colormap as those are inherited from the figure unless specifically configured for the axes.

Adam Danz on 13 Feb 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_670847

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_670847

Edited: Adam Danz on 13 Feb 2019

I've updated my solution to demonstrate how to get the handles to the axis children and copy them to a new axis. In your data, your axis only has 1 object. As Walter mentioned, if you have a bunch of objects, ax1Chil will be a vector and you can remove handles from objects you don't want to copy.

Note that you'll need to copy the objects to a cartesian axis even though your original plot appears to be polar.

Walter Roberson on 13 Feb 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_670856

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_670856

Polar Axes are a different class than non-polar axes, and require that the destination have been created with a polaraxes() call. It is not possible to just change the settings of an axes to make it handle polar coordinates.

There is another situation for the general case, which is handling duration or datetime axes. Those are not a separate class, but again you cannot just set the appropriate properties in a simple way. It turns out that you need to set the entire XRuler or YRuler to a copy of the original: you can overwrite the entire major property, as they turn out to be class objects.

Adam Danz on 13 Feb 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_670870

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_670870

When I tried to copy the data to a polar axis created by polaraxes() (last block of code in my solution), the results were not the same as in the original polar plot produced by pattern()(compare first and last figure in the image I shared). I'm not familiar with pattern() but when the 'CoordinateSystem' property is set to polar, a polar axis is created. I didn't play around with the decible conversion or normalization specified by the pattern() name-value pairs, though.

Walter Roberson on 13 Feb 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_671002

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_671002

For whatever reason, pattern() turns out not to be using polar axes, and is instead using the older polar() function that creates cartesian axes.

Adam Danz on 14 Feb 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_671272

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_671272

Edited: Adam Danz on 14 Feb 2019

Open in MATLAB Online

Interestingly, when I plot the (x,y) coordinates from the original plot onto a polar plot, it's just as distorted as when polaraxes() is used.

figure;

ax1Chil = ax1.Children;

ax2 = polar(ax1Chil.XData, ax1Chil.YData);

Copying the content of a Figure to another Figure (15)

Walter Roberson on 14 Feb 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_671380

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/444601-copying-the-content-of-a-figure-to-another-figure#comment_671380

right because it is cartesian data , the polar plot converted to cartesian already .

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABGraphics2-D and 3-D PlotsPolar Plots

Find more on Polar Plots in Help Center and File Exchange

Tags

  • plotting
  • plot
  • axes

Products

  • MATLAB
  • Phased Array System Toolbox

Release

R2018b

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Copying the content of a Figure to another Figure (17)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Copying the content of a Figure to another Figure (2024)

FAQs

How do you copy a figure into another Matlab? ›

Copy the figure to your system clipboard by clicking Edit > Copy Figure. Paste the copied figure into other applications, typically by right-clicking. By default, MATLAB® converts the background color of the copied figure to white. The Copy Figure option is not available on Linux® systems.

How to copy an object in Matlab? ›

Copy objects from one parent to another using the copyobj function. The copy differs from the original: The Parent property is now the new parent. The copied object's handle is different from the original.

How to use copyobj in Matlab? ›

new_handle = copyobj(h,p) copies one or more graphics objects identified by h and returns the handle of the new object or an array of new objects. The new graphics objects are children of the graphics objects specified by p . copyobj(___,'legacy') copies object callback properties and object application data.

How do you move axes to another figure in Matlab? ›

To achieve this programmatically, create a new empty figure and use "copyobj" to move a copy of just the axes into the new figure. You can then use "hgexport" or "print" as before. f2 = figure; copyobj(ax,f2);

How to extract data from a figure in MATLAB? ›

Direct link to this question
  1. open('2.fig');
  2. h = gcf; %current figure handle.
  3. axesObjs = get(h, 'Children'); %axes handles.
  4. dataObjs = get(axesObjs, 'Children'); %handles t.
  5. xdata = get(dataObjs, 'XData');
  6. ydata = get(dataObjs, 'YData');
Jan 30, 2019

Can I copy a figure from another paper? ›

To use a reproduced figure or table, you must obtain permission from the owner of the copyright of the original figure or table (usually the publisher), and you must also include attribution to the original source(s) in your manuscript in the Source notes below the reproduced figure or table.

How do I copy one object to another? ›

To copy objects in JavaScript, you typically have three options: using the assignment operator (=) for reference copying, performing a shallow copy using methods like Object. assign() or the spread operator (...) , and creating a deep copy using a combination of JSON. parse() and JSON. stringify() .

How do you copy and paste data in MATLAB? ›

Copy and Paste Data Using the Import Tool

Copy data into the clipboard and then import the clipboard data into MATLAB®. Select the lines below, right-click, and then select Copy. Import the clipboard data into MATLAB using the clipboard function. The Import Wizard opens.

What is the copy function in MATLAB? ›

B = copy( A ) copies each element in the array of handles A to a new array of handles B . The copy method follows these rules: The copy method does not copy dependent properties. MATLAB® does not call the copy method recursively on any handles contained in property values.

How to use Laplacian in MATLAB? ›

l = laplacian( f , v ) returns the Laplacian of the symbolic field f with respect to the vector v in Cartesian coordinates. If f is an array, then the function computes the Laplacian for each element of f and returns the output l that is the same size as f .

How to decode MATLAB code? ›

msg = decode( code , n , k ) decodes the coded message code . The function assumes that the Hamming encoding method was used, with codeword length n and message length k . msg = decode( code , n , k , codingMethod , prim_poly ) decodes code assuming that prim_poly is the primitive polynomial used for encoding.

How to use mux in MATLAB? ›

The Mux block combines inputs with the same data type and complexity into a virtual vector. You can use multiple Mux blocks to create a mux signal in stages, but the result is flat as if you used a single Mux block. Ideally, use Mux blocks to group only function-call signals.

How do you move a figure in MATLAB? ›

movegui( f , position ) moves the figure f to the specified screen location. The figure can be one created with either the figure or uifigure function. The position can be specified as a two-element vector or as a predefined position name.

How do you move an object to the axis? ›

To move objects along a specific axis

Select one or more objects to move. Place your cursor on the X, Y, or Z Axis handle of the Move gizmo. Click and drag the cursor to a new location along the selected axis.

How do you get the axes from a figure in MATLAB? ›

ax = gca returns the current axes (or standalone visualization) in the current figure. Use ax to get and set properties of the current axes. If there are no axes or charts in the current figure, then gca creates a Cartesian axes object.

How do I copy and paste a figure from MATLAB to Word? ›

Direct link to this answer
  1. MATLAB Figure window: Edit -> Copy Figure.
  2. Switch to Word and paste (ctrl + v)

How do you copy output in MATLAB? ›

Copy Selected Output and Paste as Equation Typeset

Right-click the selection and choose Copy (Ctrl + C) in the context menu. Then paste the selection as equation typeset in the live script using Ctrl + V (or right-click and select Paste).

How do you copy properties from one object to another? ›

To Copy Properties From One Object to Other Objects
  1. Click Home tab > Properties panel > Match Properties. Find.
  2. Select the object from which you want to copy properties.
  3. If you want to specify which properties are copied, enter s (Settings). ...
  4. Select the objects to which you want to copy the properties, and press Enter.

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Errol Quitzon

Last Updated:

Views: 6474

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.