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

Contact your local office

Copying the content of a Figure to another Figure (2024)
Top Articles
Latest Posts
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.