Clear Filters
Clear Filters

Graphic objects and XData and YData

117 views (last 30 days)
Eric Hong
Eric Hong on 5 Sep 2015
Commented: Mike Garrity on 9 Sep 2015
I can't even describe a problem well because of my ignorance in this subject. Here's what I'm trying to do:
I have a figure with bunch of objects on it such as text, line that contains the data being plotted, and groups created by imline.
Now, I have to perform an analysis with Xdata and YData from the axis. So, I'm doing this:
axis_h = findobj(gcf,'Type','axes');
Then, I do the following:
obj_h = get(axis_h,'Children');
So, I get the following:
3x1 graphics array:
Text (\DeltaY/\DeltaX = 27719.3352 [\DeltaX = 0.01788, \DeltaY = 495.62)
Group (imline)
Line (Fp\_ext vs. Xp\_UPR (ez5 rud revJ.dyn stiff singlePCU nom OR dmp 0p01)
Now I need to get XData and YData from Line graphic array and I don't know how.
I tried the following:
line = findobj(obj_h,'Type','line');
So, I get the following:
5x1 graphics array:
Line (Fp\_ext vs. Xp\_UPR (ez5 rud revJ.dyn stiff singlePCU nom OR dmp 0p018)
Line (end point 2)
Line (end point 1)
Line (top line)
Line (bottom line)
The same problem, I don't know how to get XData and YData from the first line object only.
I don't want to do obj_h(3) or line(1) to get XDat and YData because I don't think it's robust.
What is the robust way of getting XData and YData from "Line (Fp\_ext vs. Xp\_UPR (ez5 rud revJ.dyn stiff singlePCU nom OR dmp 0p01…)"?
Can anyone help me, please?
Thank you,
Eric
  4 Comments
Image Analyst
Image Analyst on 6 Sep 2015
Why do you not already have the x and y data? After all, you created the figure by using them, didn't you? If not, how did you lose them?
Eric Hong
Eric Hong on 9 Sep 2015
I created a plotter that you can add as many figure as you want and plot parameters on any figure of your choice. Therefore, you have to extract the data from the figure. The data is not available in the workspace.
I solved this issue by doing the following:
obj_h = get(axis_h,'Children'); obj_type = get(obj_h,'Type');
then a for loop and strcmpi(obj_type{i},'line')
Thanks,
Eric

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 5 Sep 2015
Eric - if you are plotting one or more lines or objects created by imline, then why not just save the handle to the graphics objects that you are plotting to the figure? For example,
x = -pi:0.1:pi;
y = sin(x);
h = plot(x,y);
The above code will draw the sine curve to my figure. The h is the handle to the graphics object created by plot. I can now manipulate this object as follows:
xdata = get(h,'XData');
ydata = get(h,'YData');
set(h,'YData',ydata+1);
So if you "save" the handles to these objects as you draw/plot them, then you shouldn't have to use findobj to find that handle that you are interested in.
  2 Comments
Mike Garrity
Mike Garrity on 9 Sep 2015
Yes, it is a good idea to keep the return value from plot if you're going to want to modify the data. As a wise man once said:
They're called handles because you're supposed to
hold on to them.
But if you do need to use findobj to find a handle that you haven't kept track of, then there are a lot of options that might be useful. The most common one in your situation is to have your plotting function set the Tag property of the line objects it creates. Then you can use something like this
findobj(gca,'Tag','MyPlot')
to find them.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!