필터 지우기
필터 지우기

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

조회 수: 11 (최근 30일)
Ruth Ronalter
Ruth Ronalter 2021년 12월 3일
답변: Dave B 2021년 12월 3일
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
Jon
Jon 2021년 12월 3일
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 2021년 12월 3일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Dave B
Dave B 2021년 12월 3일
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

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by