How do I use Matlab to create a boxplot?

51 views (last 30 days)
Samuel on 12 Feb 2024
Answered: Star Strider on 12 Feb 2024
I am a novice programmer trying to code a boxplot that compares pulse before for males vs. females based on the attached spreadsheet. However, I can't figure out how to delineate the variables.

Steven Lord on 12 Feb 2024
What do you mean by "code a boxplot"? Are you trying to call the boxplot function from Statistics and Machine Learning Toolbox and need to understand how to generate the correct inputs to pass into that function to create your desired diagram? Or are you trying to build the boxplot yourself from primitive objects? I strongly suspect the former, as the latter would be a more challenging task especially for a novice programmer.
If I'm correct that you're just trying to call boxplot, could you clarify the specific difficulty you're experiencing?
Are you not sure of the syntax with which to call boxplot? If so take a look at the "Create Box Plots for Grouped Data" example on the boxplot documentation page I linked above. You'd just need to determine which grouping variable from your data to use.
Are you not sure how to import the data into MATLAB in such a way that you have access to the grouping variable? If so take a look at this category page in the documentation. The Standard File Formats category is likely to have information on how to read your data from the CSV file.

Star Strider on 12 Feb 2024
There are two options, the Statistics and Machine Learning Toolbox boxplot function and the core MATLAB boxchart funciton. Both also use the Statistics and MachineLearning Toolbox findgroups function, although you can also use the unique function.
This should get you started with whatever one you want to use —
T1 = 92×8 table
PuBefore PuAfter Ran Smokes Sex Height Weight ActivityL ________ _______ _______ _______ __________ ______ ______ _________ 48 54 {'no' } {'yes'} {'male' } 68 150 1 54 56 {'no' } {'yes'} {'male' } 69 145 2 54 50 {'no' } {'no' } {'male' } 69 160 2 58 70 {'yes'} {'no' } {'male' } 72 145 2 58 58 {'no' } {'no' } {'male' } 66 135 3 58 56 {'no' } {'no' } {'female'} 67 125 2 60 76 {'yes'} {'no' } {'male' } 71 170 3 60 62 {'no' } {'no' } {'male' } 71 155 2 60 70 {'no' } {'yes'} {'male' } 71.5 164 2 60 66 {'no' } {'no' } {'female'} 62 120 2 61 70 {'no' } {'no' } {'female'} 65.5 120 2 62 76 {'yes'} {'yes'} {'male' } 73.5 160 3 62 75 {'yes'} {'no' } {'male' } 72 195 2 62 58 {'yes'} {'no' } {'male' } 72 175 3 62 100 {'yes'} {'no' } {'female'} 66 120 2 62 98 {'yes'} {'yes'} {'female'} 62.75 112 2
[ID,~,G] = unique(T1.Sex)
ID = 2×1 cell array
{'female'} {'male' }
G = 92×1
2 2 2 2 2 1 2 2 2 1
[G,ID] = findgroups(T1.Sex)
G = 92×1
2 2 2 2 2 1 2 2 2 1
ID = 2×1 cell array
{'female'} {'male' }
figure
boxplot(T1{:,1}, G)
xlabel('Sex')
ylabel('HR')
xticklabels({'Female','Male'})
title('Before')
figure
boxchart(G, T1{:,1})
xlabel('Sex')
ylabel('HR')
set(gca, 'Xtick',[1 2])
xticklabels({'Female','Male'})
title('Before')
.

Categories

Find more on Box Plots 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!