Box Plots for Vectors of Varying Length

2 次查看(过去 30 天)
Hello! I have 3 data sets named DailyTMAX_1, DailyTMAX_2, and DailyTMAX_3. Each one of them have maximum temperature data. DailyTMAX_1 from years 1931-1960, DailyTMAX_2 from 1961-1990, and DailyTMAX_3 from 1991 to 2020.
The three of them have varying lenghts. DailyTMAX_1 is 10321, DailyTMAX_2 is 11243 and DailyTMAX_3 is 10792.
I want to create a boxplot but every time I run it I get this message:
"Error: File: ClimateDataProcessor.m Line: 107 Column: 14
Invalid text character. Check for unsupported symbol, invisible character, or pasting of non-ASCII characters."
Line 107 Column 14 corresponds to g1 on the attached code
x = [DailyTMAX_1; DailyTMAX_2; DailyTMAX_3];
g1 = repmat({1931-1960},10321,1);
g2 = repmat({1961-1990},11243,1);
g3 = repmat({1991-2020},10792,1);
g = [g1; g2; g3];
boxplot(x,g)
Hope anyone can help me with this
Thank you!

采纳的回答

Joel Lynch
Joel Lynch 2021-6-22
编辑:Joel Lynch 2021-6-22
The proximate error is that the symbols around the year-range string
1931-1960
are not apostrophes, so MATLAB doesn't know what to do with them. They should appear in pink:
'1931-1960'
But that is a moot point, because you don't need to use the "g" argument in boxplot for this problem. The g argument is used to group heterogenous elements of each column in x together in seperate boxplots. You're data is already homogenous in each vector, so you can just pass three columns in x to get three boxplots. The trick is to handle their unequal lengths, which you can do by filling a just-large-enough NaN matrix x. The extra elements in each column are ignored by boxplot.
N = max( [ numel(DailyTMAX_1), numel(DailyTMAX_2), numel(DailyTMAX_3) ] ); % Longest length
x = NaN(N,3); % 3 columns large enough to fit longest vector
x(1:numel(DailyTMAX_1),1) = DailyTMAX_1;
x(1:numel(DailyTMAX_2),2) = DailyTMAX_2;
x(1:numel(DailyTMAX_3),3) = DailyTMAX_3;
boxplot(x);
You can greatly simplify this by storing "DailyTMAX_X" vectors natively in a NaN-padded x matrix.
Note: this won't add the year-labels, you can do that manually by:
set( gca, 'XTickLabel', {'1931-1960', '1961-1990', '1991-2020'} );
  4 个评论
Joel Lynch
Joel Lynch 2021-6-23
The symbol you are using appears to be the prime symbol, not the apostrophe; you'll want to check your system's keyboard layout to make sure you know which you're using.
One trick is to click on your matlab script, hit cntrl+h, and replace all ’ with '. That should remove all the bad characters from your script.
Nuria Andreu
Nuria Andreu 2021-6-23
Hi Joel thank you so much! It worked. The problem was the apostrophe. I didn't see it until you mentioned it.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by