How can I access Ydata from interaction plots?

9 Ansichten (letzte 30 Tage)
Raïsa Roeplal
Raïsa Roeplal am 28 Feb. 2023
Kommentiert: Adrsch3p am 9 Apr. 2024 um 10:40
Hi,
I am studying interaction effects of independent variables on dependent variables in Matlab using interaction plots. I have several interaction plots like this:
I want to collect the Y-values of all the plots. Therefore, I would like to create a code which can access the Ydata from each plot.
I know that [h,AX,bigax] = interactionplot(...) allows us to access the subplot axes data, but I do not know how to extract the data using these handles. Any suggestions?
  1 Kommentar
Adrsch3p
Adrsch3p am 9 Apr. 2024 um 10:40
Instead of acessing the data from interactionplot, I would suggest that you get it directly from the function grpstats. Because Y data of the interactionplot is in fact computed using grpstats.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Star Strider
Star Strider am 28 Feb. 2023
Using an example from the interactionplot documentation —
rng default; % For reproducibility
y = randn(1000,1);
% Randomly generate data for four three-level factors.
group = ceil(3*rand(1000,4));
% Display the interaction plots for the factors and name the factors 'A', 'B', 'C', 'D'.
[h,AX,bigax] = interactionplot(y,group,'varnames',{'A','B','C','D'});
Axx = findobj(h.Children, 'Type','Axes');
for k1 = 1:size(Axx,1)-1
Axk = Axx(k1);
Lines = findobj(Axk.Children,'Type','Line');
for k2 = 1:numel(Lines)
xc{k1,k2} = Lines(k2).XData;
yc{k1,k2} = Lines(k2).YData;
nc{k1,k2} = Lines(k2).DisplayName;
end
tm = [xc(k1,:);yc(k1,:);nc(k1,:)];
C(k1,:) = tm(:).';
end
% xc{:} % View Results (Optional)
% yc{:} % View Results (Optional)
% nc{:} % View Results (Optional)
DNC = [compose('X%d',1:3); compose('Y%d',1:3); compose('DisplayName%d',1:3)];
Results = cell2table(C, 'VariableNames',DNC(:).')
Results = 16×9 table
X1 Y1 DisplayName1 X2 Y2 DisplayName2 X3 Y3 DisplayName3 ___________ ______________________________________ ____________ ___________ ___________________________________ ____________ ___________ ______________________________________ ____________ 1 2 3 -0.10012 -0.14598 0.16843 {'A = 3' } 1 2 3 0.05729 -0.13914 -0.061251 {'A = 2' } 1 2 3 -0.082502 0.061337 0.0041465 {'A = 1' } 1 2 3 -0.10012 -0.14598 0.16843 {0×0 char} 1 2 3 0.05729 -0.13914 -0.061251 {0×0 char} 1 2 3 -0.082502 0.061337 0.0041465 {0×0 char} 1 2 3 -0.11877 -0.091477 0.0791 {0×0 char} 1 2 3 -0.098697 -0.093319 0.031328 {0×0 char} 1 2 3 -0.035499 0.011612 0.00083478 {0×0 char} 1 2 3 0.046984 -0.052158 -0.10695 {0×0 char} 1 2 3 -0.040539 -0.16683 0.04365 {0×0 char} 1 2 3 -0.044517 -0.13393 0.16396 {0×0 char} 1 2 3 0.0041465 -0.061251 0.16843 {0×0 char} 1 2 3 0.061337 -0.13914 -0.14598 {0×0 char} 1 2 3 -0.082502 0.05729 -0.10012 {0×0 char} 1 2 3 8.8326e-05 0.021481 0.071188 {'B = 3' } 1 2 3 -0.24391 -0.18516 0.11863 {'B = 2' } 1 2 3 -0.0070887 -0.020265 -0.09547 {'B = 1' } 1 2 3 8.8326e-05 0.021481 0.071188 {0×0 char} 1 2 3 -0.24391 -0.18516 0.11863 {0×0 char} 1 2 3 -0.0070887 -0.020265 -0.09547 {0×0 char} 1 2 3 0.14227 -0.10678 0.057161 {0×0 char} 1 2 3 -0.17583 -0.15826 0.044711 {0×0 char} 1 2 3 -0.018925 -0.084299 -0.015907 {0×0 char} 1 2 3 0.00083478 0.031328 0.0791 {0×0 char} 1 2 3 0.011612 -0.093319 -0.091477 {0×0 char} 1 2 3 -0.035499 -0.098697 -0.11877 {0×0 char} 1 2 3 -0.09547 0.11863 0.071188 {0×0 char} 1 2 3 -0.020265 -0.18516 0.021481 {0×0 char} 1 2 3 -0.0070887 -0.24391 8.8326e-05 {0×0 char} 1 2 3 -0.010002 -0.035674 0.17971 {'C = 3' } 1 2 3 0.020492 -0.13946 -0.04292 {'C = 2' } 1 2 3 -0.044294 -0.19478 -0.041124 {'C = 1' } 1 2 3 -0.010002 -0.035674 0.17971 {0×0 char} 1 2 3 0.020492 -0.13946 -0.04292 {0×0 char} 1 2 3 -0.044294 -0.19478 -0.041124 {0×0 char} 1 2 3 0.16396 0.04365 -0.10695 {0×0 char} 1 2 3 -0.13393 -0.16683 -0.052158 {0×0 char} 1 2 3 -0.044517 -0.040539 0.046984 {0×0 char} 1 2 3 -0.015907 0.044711 0.057161 {0×0 char} 1 2 3 -0.084299 -0.15826 -0.10678 {0×0 char} 1 2 3 -0.018925 -0.17583 0.14227 {0×0 char} 1 2 3 -0.041124 -0.04292 0.17971 {0×0 char} 1 2 3 -0.19478 -0.13946 -0.035674 {0×0 char} 1 2 3 -0.044294 0.020492 -0.010002 {0×0 char} 1 2 3 0.16396 0.04365 -0.10695 {'D = 3' } 1 2 3 -0.13393 -0.16683 -0.052158 {'D = 2' } 1 2 3 -0.044517 -0.040539 0.046984 {'D = 1' }
This returns cell arrays of the x-data (‘xc’) , y-data (‘yc’), and ‘DisplayName’ (‘nc’) for each line in the nested plots. Address them by their row and column numbers. To find out what are in the various cells, use the ‘nc’ information and then use those row-column indices to find the corresponding ‘xc’ and ‘yc’ information.
The table isn’t perfect, since the first line of every set is duplicated, however it should be enough to get the information yuou want.
I use the example from the documentation to test the code, and since I have never previously used interactionplot, I have no experience with it. I have no idea how fragile the code may be to other data sets.
.

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by