Box Plots for Vectors of Varying Length

조회 수: 2 (최근 30일)
Nuria Andreu
Nuria Andreu 2021년 6월 21일
댓글: Nuria Andreu 2021년 6월 23일
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