hist error: Input arguments must be numeric, but all my input is numeric?

7 views (last 30 days)
Hello,
I'm using hist as part of a for loop to retrieve participant numerical data with Matlab. The part of the for loop in question looks like this (Note: the actual for loop is much longer, but this is the part that comes back with an error.
for folder = 3:length(contents)
T = readtable(filename)
T_target_mask = T((strcmp(T.item, 'LowLeft.png') |...
strcmp(T.item, 'LowRight.png') |...
strcmp(T.item, 'HighRight.png') | ...
strcmp(T.item, 'HighLeft.png')),:)
T_target_mask = T_target_mask(strcmp(T_target_mask.mask, 'mask.png'), :);
[count_target_mask, rating] = hist(T_target_mask.Button_press_1_, [0:3])
count_all_target_mask = cat(1, count_all_target_mask, count_target_mask)
cd ..
end
basically, it reads a table of data (named filename in this case), searches for the target (named Lowright, Lowleft, etc.), checks that it has a mask, and then it orders the data based on the rating between 0 and 3. But all of the data it searches for is numerical, and I have run every other subject file besides this one, and none of the others have the issue. I noticed when it tries to output at the second loop, the button presses are all in quotes ('0','1','2','3'), so I guess it's trying to read the file as a string, but why and how to put it back, I'm unsure. Any help would be appreciated
  2 Comments
Jon
Jon on 3 Dec 2021
It would be helpful if you could make a self contained example, that demonstrates the problem, please attach code and datafile (if needed for example to run)
Stephen23
Stephen23 on 3 Dec 2021
Note that
for folder = 3:length(contents)
is most likely a non-robust attempt to handling the dot-folder names. Better to use SETDIFF or ISMEMBER.

Sign in to comment.

Answers (1)

Dave B
Dave B on 3 Dec 2021
From your description it sounds like when you called readtable, it read the values as strings rather than numbers. When you call readtable without any details on what types the variables should be loaded as, MATLAB takes a guess, and it sounds like for one of your files MATLAB guessed incorrectly.
One option here is to specify the type for readtable. There are some nice examples in the readtable documentation (check out the section "Detect and Use Import Options for Text Files" or "Detect and Use Import Options for Spreadsheet Files" as appropriate.
That might be a little heavyweight feeling of a solution. Another strategy - check if the values are a char, and if so do convert them. Something like
if iscellstr(tbl.foo)
tbl.foo = str2double(tbl.foo)
end

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!