Creating M histograms from an NxM table on separate plots

13 views (last 30 days)
Hi there,
I have an N x M table and I am trying to create M histograms on different plots, I can't seem to figure out how to plot them on different figures. If I hold on, they all plot on the same axis'
data = readtable('file.csv'); % Import in wine raw data
predictors = data(:,1:width(data)-1); % remove quality as it is our output
headers = predictors.Properties.VariableNames; % Get predictor labels
%% Lets check to see how our predictors are distributed
statarray = grpstats(predictors,[],{'mean','std'})
for k = width(headers);
a = table2array(predictors(:,k)); % Converting from table to array for hist
histogram(a)
title(headers(1,k))
end
I'm also not sure why I can't pass a table row to a histogram and need to table2array the data.
Any help would be greatly appreciated. Thank you

Accepted Answer

Steven Lord
Steven Lord on 26 Nov 2020
A = 2*randn(1e4, 6) + 5*(0:5);
T = array2table(A);
for k = 1:width(T)
subplot(2, 3, k)
histogram(T{:, k})
end
Note that each histogram is roughly centered around a different multiple of five, but each has (roughly) the same shape.

More Answers (1)

dpb
dpb on 26 Nov 2020
Edited: dpb on 27 Nov 2020
...
for k=1:width(headers) % k=width(headers) --> one value, the last column
figure % create a new figure for each histogram plot
histogram(predictors.(predictors.Properties.VariableNames(k)); % hist each variable
title(predictors.Properties.VariableNames{k})
end
using your secondary table. There's no reason to duplicate data, however, just use the variables of interest in the original table:
data = readtable('file.csv'); % Import in wine raw data
statarray=grpstats(data,[],{'mean','std'},'DataVars'data.Properties.VariableNames(1:width(data)-1));
for k=1:width(1:width(data)-1))
figure % create a new figure for each histogram plot
histogram(data.(data.Properties.VariableNames{k}); % hist each variable
title(data.Properties.VariableNames{k})
end
"not sure why I can't pass a table row to a histogram"
It's not a row but a column...but, that aside, you referenced the table with () subscripting which returns another table. To dereference the table use {} subscripting similar to a cell array. Or use "dot" addressing with a table variable name as illustrated above. There is a veritable cornucopia of addressing modes possible with tables; see <Access-data-in-a-table> for the details.
  2 Comments
Jeremy Brecevic
Jeremy Brecevic on 26 Nov 2020
Edited: Jeremy Brecevic on 26 Nov 2020
Hi, Thanks for this, unfortunately I run into the same issue as before I table2array'd the data where I get a type mismatch when trying to use dots notation
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
Using Figure, I also only get the last chart to display.
dpb
dpb on 26 Nov 2020
Edited: dpb on 26 Nov 2020
Show your code...and error in context. The above worked here with a table.
You'll get as many figures as times through the loop...if it aborts early, then it's obvious you won't get them all. And, of course, each figure is opened on top of (or nearly so) so you may only see the one even though they're all there.
Steven Lord's idea of subplot may be what you were thinking of as desired format -- I took the request as stated literally as one/figure. With a little table that happened to be here already--
>> t
t =
10×5 table
Time P1 P2 V S
____ ___ ______ ______ ______
0.01 100 14.142 29.022 5.3872
0.02 110 14.832 30.555 5.5276
0.03 120 15.492 32.019 5.6585
0.04 130 16.125 33.423 5.7813
0.05 140 16.733 34.774 5.897
0.06 150 17.321 36.078 6.0065
0.07 160 17.889 37.338 6.1105
0.08 170 18.439 38.559 6.2096
0.09 180 18.974 39.745 6.3044
0.1 190 19.494 40.898 6.3952
>> figure
>> j=0;
>> for i=2:width(t)
j=j+1;
subplot(2,2,j)
histogram(t.(t.Properties.VariableNames{j}))
end
>>
resulted in the following:
Not all that interesting, but proves the code as written will work if you don't make some other syntax error.
Steven's code illustrated the alternative of {} dereferencing by column number rather than variable name.
But, the key point is, you MUST use a form that returns the dereference data from the table, not another table. As the referenced link to the documentation shows, there are many ways to address a table; many of them return another table; that appears to be the error you're still making.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!