Histogram won't plot decimals
Show older comments

Hello, This is a screenshot of a table I have constructed for work.
Just to play it safe, I blacked out the column names, though it would be hard to assume anything with just 7 rows of the table to go off of. We will call the 5 fields "column1, column2, etc."
If I do:
histogram([a(1:135756).column1])
It creates a histogram wilth all 135,756 points. Works beautifully.
If I do:
histogram([a(1:135756).column4])
I get an error saying "x must be a numeric column vector."
I assume it has something to do with those symbols next to where I wrote "1, 2, 3, 4, 5" in each column. I don't know what each of them mean. Column 4 and 5 are not the same as 1, 2, & 3 because they include decimals, therefore a histogram of column 1 and 2 work great, but 4 and 5 produce that error.
How would I fix this so I can do a histogram of column 4 and 5, with decimals.
Thanks!
Accepted Answer
More Answers (1)
The problem is likely caused by at least one of the elements in column 4 that is not numeric. The non-numeric value could be part of the actual raw data or it could be an artifact created when the data are read into Matlab. The symbols you pointed out indicate that the columns have mixed data types.
Here's a demo that illustrates the problem.
a(1).c = 1.234;
a(2).c = pi;
a(3).c = '0'; % Note, this is not numeric
a(4).c = 2.814;
a(5).c = {0}; % Note, this is not numeric
When those values are concatenated, they are clearly not numeric,
[a.c]
ans =
1×5 cell array
{[1.2340]} {[3.1416]} {'0'} {[2.8140]} {[0]}
which causes the error
histogram([a.c])
Error using histogram
Expected input number 1, x, to be one of these types:
double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64, logical, datetime, duration
Instead its type was cell.
or
[a(1:4).c]

which causes the error
histogram([a(1:4).c])
Error using histogram>parseinput (line 300)
Trailing input arguments must occur in name-value pairs.
The error message you shared, "x must be a numeric column vector", is odd for your Matlab release (r2020a). As you can see in the error message above, the x-input to histogram can be non-numeric (ie, datetime, duration, logical, etc.). Seeing the full error message and the entire line that calls histrogram() would be helpful. It would also be helpful to see the first few elements of the output to a(1:135756).column4.
8 Comments
Sclay748
on 19 Aug 2020
To create an index of non-numeric values,
idx = ~arrayfun(@(i)isnumeric(a(i).column4),1:numel(a));
optional: find(idx)
To see the non-numeric values,
a(idx).column4
"What do you mean show the output a(1:135756).column4?"
z = [a(1:135756).column4];
disp(z(1:20))
but I don't need to see that any more. We now know that it produces a char array.
Sclay748
on 19 Aug 2020
Sclay748
on 20 Aug 2020
Adam Danz
on 21 Aug 2020
Looks like Steven Lord addressed your question.
Categories
Find more on Logical 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!