Histogram won't plot decimals

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

What does this command display?
class([a(1:135756).column4])
My guess is that it will probably display something like char or string rather than double or single.

7 Comments

ans = 'char'
and a blank graph
Sounds like at least one of the values in colum 4 is a character.
a(1).c = 1.234;
a(2).c = pi;
a(3).c = '0'; % Note, this is not numeric
a(4).c = 2.814;
Is there an easy/faster way of finding the character then going through each point one by one?
>> 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
>> isCNumeric = arrayfun(@(x) isnumeric(x.c), a)
isCNumeric =
1×5 logical array
1 1 0 1 0
Or you could use ischar instead of isnumeric to identify those elements of a whose field c is a char array. You could try converting those to double using str2double, but if you do you probably then want to watch out for NaN values.
str2double('8.19.2020') % NaN
str2double('8.19') % 8.19
Thank you, Steven! Everything works now. Was able to find the not numeric easily.
Another question, using the same table as above. How would I do the mean of a column?
I did:
x = mean(a.column1)
but it says too many input arguments.
Usually I have no problem with using mean, but is it possisble that this is just too big of a set of data?
You don't have a table array. You have a non-scalar struct array.
When you write a.column1 for a non-scalar struct a it makes a comma-separated list.
a = struct('column1', {1, 2:3, 4:6}) % Makes a 3 element struct
a.column1
Note that the second line displays three separate ans values. For your struct array with 135k elements, that expression would display 135k separate ans values, and if you wrote:
mean(a.column1)
that's the equivalent of:
mean(1, 2:3, 4:6)
mean doesn't have a syntax that matches that particular combination of inputs. It certainly doesn't have a syntax that accepts 135k inputs!
Concatenate the elements of that comma-separated list together and pass that vector into mean.
x1 = [1, 2:3, 4:6]
x2 = [a.column1]
mean(x2)
mean([a.column1])

Sign in to comment.

More Answers (1)

Adam Danz
Adam Danz on 19 Aug 2020
Edited: Adam Danz on 19 Aug 2020
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

ahh so somewhere in those 135,756 points, there isn't a numeric. That will be fun to find.
What do you mean show the output a(1:135756).column4?
Nothing outputs because of the error.
Sclay748
Sclay748 on 19 Aug 2020
Edited: Sclay748 on 19 Aug 2020
Is there an easy/faster way of finding the character then going through each point one by one?
If it is any easier, I have all 135,756 points in an excel document, but it still will take a while to go through unless there is an easier command.
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
Sclay748 on 19 Aug 2020
Edited: Sclay748 on 19 Aug 2020
ahhhh found it. One calculation had a 0, so it created an error when dividing by 0. Let me run the histogram function and hope it works.
That find command was a life saver!
I'll let you know.
Thanks!
Adam Danz
Adam Danz on 19 Aug 2020
Edited: Adam Danz on 19 Aug 2020
Check out Steven Lord's version of the arrayfun. It's cleaner than mine. We all learn from Steven ;)
everything works now, thank you so much!
Steven's may be cleaner, but yours got the job done as well haha. Saved me from looking though all those points.
Another question, using the same table as above. How would I do the mean of a column?
I did:
x = mean(a.column1)
but it says too many input arguments.
Usually I have no problem with using mean, but is it possisble that this is just too big of a set of data?
Looks like Steven Lord addressed your question.

Sign in to comment.

Products

Release

R2020a

Asked:

on 19 Aug 2020

Commented:

on 21 Aug 2020

Community Treasure Hunt

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

Start Hunting!