How can I access Ydata from interaction plots?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Adrsch3p
2024-4-9
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.
回答(1 个)
Star Strider
2023-2-28
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(:).')
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.
.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!