I would like to find and plot the means and standard deviations for lots of columns of data.

3 views (last 30 days)
I have gone over and over this, but I can't seem to figure out what is going on. Eventually, I would like to plot the mean of 3 columns of data, based on their category. There are four categories, Spring, Summer, Winter, Autumn. I can import the data from excel, find the variable names, and recreate the table with just the data I want, but I can't seem to find a way to calculate the mean based on the category. For example, I want to find the mean for all Spring for each of the final three columns. This is just one set of data. There are 8 total sets (all different lengths), so eventually, I would like to plot all of them, but I thought I would just start with 1 set. At this stage, if I can get to show the mean for table A, that would be great. Please help me get the means and standard deviations.
T = readtable('SdAlphabetSeasons.xlsx');
T.Properties.VariableNames
Season = T(:,4);
dD = T(:,5);
d18O = T(:,6);
xs = T(:,7);
A = table(Season,dD,d18O,xs);
SSNMean = grpstats(A,"Season");
Function 'subsindex' is not defined for values of class 'string'.
Error in dsgrpstats (line 97)
[group,glabel,groupname] = mgrp2idx(a_data(groupvars),a_nobs);
Error in grpstats (line 136)
[varargout{1:nargout}] = dsgrpstats(x,group,whichstats,varargin{:});

Answers (1)

Umar
Umar on 21 Jul 2024
Moved: Walter Roberson on 21 Jul 2024
Hi Cg Gc,
Try using 'grpstats' function, it will help you to calculate the mean based on categories. However, analyzing your code, I came across the error message "Function 'subsindex' is not defined for values of class 'string'" which indicates that there might be a data type mismatch. So, I will suggest to make sure that the 'Season' column in your table is of the correct data type for grouping. You can convert it to a categorical variable using the 'categorical' function:
A.Season = categorical(A.Season);
SSNMean = grpstats(A, 'Season');
By converting the 'Season' column to a categorical variable, you should be able to group the data correctly and calculate the mean for each season in the final three columns. This should help you get the means and standard deviations for your dataset. For more information on these functions, please refer to,
I hope this answers your question.
  2 Comments
Cg Gc
Cg Gc on 21 Jul 2024
Matlab said No. This is similar to the errors I was getting before. Could it be related to the version I am running?
T = readtable('SdAlphabetSeasons.xlsx');
Season = T(:,4);
dD = T(:,5);
d18O = T(:,6);
xs = T(:,7);
A = table(Season,dD,d18O,xs);
A.Season = categorical(A.Season);
SSNMean = grpstats(A,'Season');
Error using categorical (line 277)
Conversion from table not supported. Extract data from one or more table variables into an array using dot or brace subscripting. Then
convert the array to a categorical array.
Umar
Umar on 21 Jul 2024
Edited: Umar on 21 Jul 2024

Hi Cg Gc,

Basically the error message indicates that conversion from a table is not supported, suggesting that the issue lies in trying to directly convert a table variable to a categorical array. So, you have to extract data from the table into an array before converting it to a categorical array.Try extracting the data from the 'Season' column using dot or brace subscripting. For example, you can do this by using:

seasonData = A.Season;

Then, convert the extracted data into an array before converting it to a categorical array. This can be done as follows

seasonArray = table2array(seasonData);

seasonCategorical = categorical(seasonArray);

I just experimented with my code and was able to print out means and standard deviations. Please see attached results.

If you encounter any further issues or need additional assistance, don't hesitate to reach out for further guidance.

Sign in to comment.

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!