Error using gscatter. There must be one value of G for each row of X.

24 views (last 30 days)
Hi,
I am trying to make a figure of 3 variables. As per description of gscatter, length of x and y vectors should be same (in my code it's same). My g here is mean of each bin. Here is my code:
Data1=readtable('F:\Excel_file\Results.xlsx', 'Sheet', 1, 'ReadVariableNames', true, 'VariableNamingRule','preserve');
Data = table2array(Data1)
Y = Data(:,1); 5000x1 double
X = Data(:,2); 5000x1 double
edges = 0:10:40;
Coarseaer_Bin=discretize(Data1.aer,edges);
%Find the mean, Std for each bin
fnstdMn=@(x)std(x)./sqrt(size(x,1));
tStats=grpstats(Data,'Coarseaer_Bin',{'mean','std',fnstdMn});
tStats.Properties.VariableNames=strrep(tStats.Properties.VariableNames,'Fun3','stdMn');
% plot the data
g = gscatter(X,Y,tStats.mean_Coarseaer_Bin)
When I try to plot it gives me error, 'There must be one value of G for each row of X'. I don't know what I'm missing here. Any help would be appreciated. Thank you

Answers (1)

Walter Roberson
Walter Roberson on 10 Mar 2023
tStats=grpstats(Data,'Coarseaer_Bin',{'mean','std',fnstdMn});
tStats.Properties.VariableNames=strrep(tStats.Properties.VariableNames,'Fun3','stdMn');
Those calls and assignments in that form are only valid when Data is a table. However
Data = table2array(Data1);
so Data is not a table.
You would not reach the gscatter because of those errors.
If your actual code is processing Data1 instead of Data, then the result mean that you get out would be a mean per group, not a mean per row of input. But you gscatter specifying the means as the group variable. When you use gscatter then the group input parameter must effectively be the class number or class label for each row.
  3 Comments
Zhou Ci
Zhou Ci on 10 Mar 2023
Secondly if gscatter doesn't work here. Is there any other function which I can use here? I have X and Y vectors of the same length and in the figure I want to plot the mean of each bin of my 3rd variable (Data1.aer here).
Walter Roberson
Walter Roberson on 10 Mar 2023
You have (implicitly) a bin number for each bin. That can be the group number of gscatter purposes.
You have a mean Coarseaer_Bin for each of the bins. That could be one of the two variables (X or Y)
You need a second variable for scatter purposes. I do not know what you would use for that.
If you were to take mean X and mean Y for the bin, that would be two variables, and then how would you represent mean Coarseaer_Bin ? Spot size? Color? Different marker shape for each bin and the marker color encodes intensity of mean Coarseaer_Bin ??
I wonder if you should be looking at using boxplot ?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!