1

I want to extract data from a figure similar to the figure in the answer of this question How can I adjust 3-D bar grouping and y-axis labeling in MATLAB?

I use the following MATLAB code, however it returns null for x, y, and z

x=get(findobj(get(gca,'Children'),'type','line'),'xdata');
y=get(findobj(get(gca,'Children'),'type','line'),'ydata');
z=get(findobj(get(gca,'Children'),'type','line'),'zdata');

Would you please help me to find the problem? It works for 2D plot but it does not work for a 3D plot like:

bar3([3 2 4;2 3 1]);

I do not understand why?

i also tried the following code:

D=get(gca,'Children'); 
XData=get(D,'XData');
YData=get(D,'YData');
ZData=get(D,'ZData');

in this case i get some answer for ZData which is a 3*1 cell, each cell contain an array of 12*4. still do not know how to extract the values and what is this cell?

As mentioned in the comments ZData has a regular pattern. Using the following code I could extract the data :

z=ZData;
a=z{1,1};
r=size(a,1);
num_rows=ceil((r-2)/6);
num_col=size(z,1);
result=zeros(num_rows,num_col);
m=0;
for  i=size(z,1):-1:1
    m=m+1;
    z_i=z{i,1};
    k=0;
    for j=2:6:r
        k=k+1;         
        result(k,m)=z_i(j,2);      
    end    
end
end

result would be the data we are looking for.

Community
  • 1
  • 1
Julie
  • 11
  • 2
  • because `bar` type of graphic object are different from `line`. Your code cannot find `line` object when there isn't any. Did you generate the bar plot yourself ? If yes you should have the data available already anyway. – Hoki Aug 10 '15 at 09:59
  • thanks no i just have the 3d plot, so you mean i should replace line by bar ? it did not work. – Julie Aug 10 '15 at 10:00
  • The function `bar3` generate `surface` type of object, one for each data set (so 3 objects in your case). The data you are interested in are in the `ZData` properties of your object, but you'll have to dig inside to find just the few values you are interested in. – Hoki Aug 10 '15 at 10:06
  • thanks that worked, but the problem is ZData would be a 3*1 cell, each cell contain an array of 12*4 (for the simple example bar3([3 2 4;2 3 1])) This is a small example that i can see the values. but i have lots of 3D bar plots that i need to extract their data and i cannot check them one by one. – Julie Aug 10 '15 at 10:14
  • There is a regular pattern in how the `ZData` matrix is organized. you have to find on your small example how the bar _height_ data can be found in the matrix, understand how the `ZData` matrix is organized, then you'll be able to pick the value you want in your larger example. – Hoki Aug 10 '15 at 10:19
  • thanks a lot i did not know there is a regular pattern – Julie Aug 10 '15 at 10:21
  • Just another question, my figures are more like the figure in the answer of this question http://stackoverflow.com/questions/3601434/how-can-i-adjust-3-d-bar-grouping-and-y-axis-labeling-in-matlab i mean it has been grouped. does the ZData in this fgure have the same regular pattern? – Julie Aug 10 '15 at 11:08

0 Answers0