Plot data within a table and use categorial column to "split" them in the graph

8 views (last 30 days)
Hello guys,
I have a table.
The size is 795183x35 (in the original data).
The column 3 "RotorNumber" is type categorial.
How can I plot (it doesn´t matter which plot but e.g. quiver) the data of the other rows but by considering the categories so i can have a legend with the different categories?
My first idea was to make a for-loop and to split the table by comparing the categorial data... but for sure there is a much easier way..??
I never worked with categorial data so I don´t know how to handle these... .
Best regards...
and thanks in advance.
  2 Comments
the cyclist
the cyclist on 2 Dec 2022
Edited: the cyclist on 2 Dec 2022
This would be easier to help with if you posted a small sample of the table (e.g. maybe 10 rows, with a few different categories of RotorNumber). You can used the paper clip icon in the INSERT section of the toolbar to upload it here.

Sign in to comment.

Answers (1)

Seth Furman
Seth Furman on 7 Dec 2022
Group data by RotorNumber using findgroups
load MiniExample.mat
t = sortrows(ExampleTable)
t = 15×35 table
datetime a RotorNumber Runs b c d e f g h i j k datetimeright m n o p q r s t u v w x y z aa bb cc dd ee ff ___________________ ___ __________________________ ____ ______ ___ ______ ___ ______ __ _ ____ _____ _ ___________________ _____ ___ _____ ___ ________ ________ ________ ________ ________ _______ ________ ________ ________ ________ _______ _______ __________ ________ ________ _________ 2022-03-21T15:00:08 100 102A3C02920101000220730650 1 180.99 33 58.59 191 DUO2_1 13 2 135 67.5 1 2022-03-21T15:00:46 30.37 4 19.55 145 151.79 98.574 30.296 2.1185 -57.514 -11.179 -16.014 11.213 121.49 96.456 -41.499 -22.393 0.19959 0.021491 0.27845 -1.003 2022-03-21T15:00:46 100 102A3C02920101000220730650 2 30.37 4 19.55 145 DUO2_1 12 4 22.5 67.5 2 2022-03-21T15:01:12 5.15 353 2.44 107 30.296 2.1185 5.1116 -0.62763 -16.014 11.213 -0.71339 2.3334 25.184 2.7461 -15.301 8.88 0.16872 -0.29626 0.044547 0.20809 2022-03-21T15:01:12 100 102A3C02920101000220730650 3 5.15 353 2.44 107 DUO2_1 2 1 22.5 67.5 2 2022-03-21T15:01:30 0.57 323 2.51 295 5.1116 -0.62763 0.45522 -0.34303 -0.71339 2.3334 1.0608 -2.2748 4.6564 -0.28459 -1.7742 4.6082 0.089056 0.54656 -1.487 -0.97491 2022-03-21T15:03:16 100 102A3C02920101000220730657 1 157.09 26 86.09 169 DUO2_1 12 3 135 67.5 1 2022-03-21T15:03:52 12.75 310 23.81 111 141.19 68.864 8.1955 -9.7671 -84.508 16.427 -8.5327 22.229 133 78.631 -75.976 -5.8018 0.058046 -0.14183 0.10097 1.3532 2022-03-21T15:03:52 100 102A3C02920101000220730657 2 12.75 310 23.81 111 DUO2_1 5 5 22.5 67.5 2 2022-03-21T15:04:18 1.98 192 1.47 331 8.1955 -9.7671 -1.9367 -0.41167 -8.5327 22.229 1.2857 -0.71267 10.132 -9.3554 -9.8184 22.941 -0.23632 0.042148 -0.15068 -0.032061 2022-03-21T15:06:41 100 102A3C02920101000220730664 1 138.97 19 213.92 165 DUO2_1 10 5 135 112.5 1 2022-03-21T15:07:16 32.46 287 35.04 75 131.4 45.244 9.4904 -31.042 -206.63 55.367 9.069 33.846 121.91 76.286 -215.7 21.521 0.072226 -0.68609 -0.04389 0.61131 2022-03-21T15:07:16 100 102A3C02920101000220730664 2 32.46 287 35.04 75 DUO2_1 13 6 22.5 67.5 2 2022-03-21T15:07:44 2.25 202 5.49 26 9.4904 -31.042 -2.0862 -0.84286 9.069 33.846 4.9344 2.4067 11.577 -30.199 4.1346 31.439 -0.21982 0.027153 0.54409 0.071106 2022-03-21T15:07:44 100 102A3C02920101000220730664 3 2.25 202 5.49 26 DUO2_1 1 2 22.5 67.5 3 2022-03-21T15:08:02 2.32 20 0.84 21 -2.0862 -0.84286 2.1801 0.79349 4.9344 2.4067 0.78421 0.30103 -4.2663 -1.6364 4.1502 2.1056 -1.045 -0.94142 0.15893 0.12508 2022-03-21T15:11:33 100 102A3C02920101000220730671 1 97.73 249 132.7 190 DUO2_1 7 3 135 112.5 1 2022-03-21T15:12:03 16 289 57.03 197 -35.023 -91.239 5.2091 -15.128 -130.68 -23.043 -54.538 -16.674 -40.232 -76.111 -76.146 -6.3692 -0.14873 0.16581 0.41733 0.7236 2022-03-21T15:12:03 100 102A3C02920101000220730671 2 16 289 57.03 197 DUO2_1 6 2 22.5 67.5 1 2022-03-21T15:12:28 6.31 314 16.95 201 5.2091 -15.128 4.3833 -4.539 -54.538 -16.674 -15.824 -6.0743 0.8258 -10.589 -38.714 -10.6 0.84147 0.30004 0.29015 0.3643 2022-03-21T15:12:28 100 102A3C02920101000220730671 3 6.31 314 16.95 201 DUO2_1 3 4 22.5 67.5 2 2022-03-21T15:12:53 1.86 201 2.08 354 4.3833 -4.539 -1.7365 -0.66656 -15.824 -6.0743 2.0686 -0.21742 6.1198 -3.8725 -17.893 -5.8569 -0.39615 0.14685 -0.13072 0.035793 2022-03-23T13:14:46 100 102A3C02920101000220730666 1 81.6 44 214.78 166 DUO2_1 6 5 135 112.5 1 2022-03-23T13:15:17 34.25 312 26.34 107 58.698 56.684 22.918 -25.453 -208.4 51.96 -7.7011 25.189 35.78 82.137 -200.7 26.771 0.39043 -0.44903 0.036953 0.48478 2022-03-23T13:15:17 100 102A3C02920101000220730666 2 34.25 312 26.34 107 DUO2_1 14 6 22.5 67.5 2 2022-03-23T13:15:44 1.31 262 5.65 292 22.918 -25.453 -0.18232 -1.2973 -7.7011 25.189 2.1165 -5.2386 23.1 -24.155 -9.8176 30.428 -0.0079553 0.050967 -0.27484 -0.20797 2022-03-23T13:15:44 100 102A3C02920101000220730666 3 1.31 262 5.65 292 DUO2_1 1 2 22.5 67.5 3 2022-03-23T13:16:03 1.16 67 2.23 288 -0.18232 -1.2973 0.45325 1.0678 2.1165 -5.2386 0.68911 -2.1209 -0.63556 -2.365 1.4274 -3.1177 -2.486 -0.82311 0.32558 0.40485 2022-03-23T13:19:19 100 102A3C02920101000220730668 1 116 94 59.36 250 DUO2_1 9 2 135 67.5 1 2022-03-23T13:19:50 17.73 66 25.38 226 -8.0918 115.72 7.2114 16.197 -20.302 -55.78 -17.63 -18.257 -15.303 99.52 -2.6719 -37.523 -0.89121 0.13997 0.86839 0.3273
[groupNums,uniqueRotorNumbers] = findgroups(t.RotorNumber)
groupNums = 15×1
1 1 1 2 2 3 3 3 6 6
uniqueRotorNumbers = 6×1 categorical array
102A3C02920101000220730650 102A3C02920101000220730657 102A3C02920101000220730664 102A3C02920101000220730666 102A3C02920101000220730668 102A3C02920101000220730671
Plot data using splitapply and plot
hold on
splitapply(@plot,t(:,"b"),groupNums)
hold off
legend(string(uniqueRotorNumbers));
title("b vs. Row Number");
ylabel("b");
xlabel("Row Number");
figure
hold on
splitapply(@(x,y)plot(x,y),t(:,["datetime","b"]),groupNums)
hold off
legend(string(uniqueRotorNumbers));
title("b vs. datetime");
ylabel("b");
xlabel("datetime");
Plot data using splitapply and stackedplot support for multiple table inputs
tableCellData2Table = @(varargin){table(varargin{:},VariableNames=t.Properties.VariableNames)};
tCell = splitapply(tableCellData2Table,t,groupNums)
tCell = 6×1 cell array
{3×35 table} {2×35 table} {3×35 table} {3×35 table} {1×35 table} {3×35 table}
variablesToPlot = t.Properties.VariableNames(5:6);
stackedplot(tCell,variablesToPlot,LegendLabels=string(uniqueRotorNumbers))
OR you can set the x-variable with the XVariable parameter.
stackedplot(tCell,variablesToPlot,LegendLabels=string(uniqueRotorNumbers),XVariable="datetime")

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!