How to make error checking for each element in array?

6 次查看(过去 30 天)
Hello guys, I am trying to get Matlab to check for errors for an input. The input can be entered in as an array and I would like for it to check each element if it is a negative value or non-numerical.
Here is what I have so far, it works if only a single number is entered but otherwise it crashes.
function [ volume ] = cube( a )
%p=imread('cubepict.png');
%imshow(p)
repeat = 1;
while repeat ==1
a = input ('Please enter a value or values for a according to the diagram: ', 's');
if any(~isnan(str2double(a)))
a = any(str2double(a));
end
if any(a < 0) |any(ischar(a))
disp ('Input(s) must be numerical and cannot contain negative values. Please try again.')
else repeat=0;
end
end
fid=fopen('cubelength.txt','w') ;
fprintf(fid, '%1.2i',a);
fclose(fid);
load cubelength.txt;
volume = a.^3;
fid2=fopen('volume.txt','w');
fprintf(fid2,'%1.2i',volume);
end

回答(3 个)

Walter Roberson
Walter Roberson 2011-11-30
After you read "a" but before you do anything else to it:
a = regexp(a, ' ', 'split');
This will turn "a" in to a cell array of strings. This is needed because str2double() can only return one value per input string but is happy to do that for each cell array member.
Warning: your line
a = any(str2double(a));
is badly broken. It will set a to the scalar value 1 if at least one field in the input was a number, and will return 0 if the input was empty or if all of the fields in the input were non-numbers. Thus will thus turn the array of values in "a" in to a single (logical) value, which will not be useful for further processing.
  3 个评论
Walter Roberson
Walter Roberson 2011-12-1
What you need to know for that message is whether any of the conversions failed or resulted in a value less than 0.
You will find this logically easier to process if you do not keep overwriting a.
A_num = str2double(a);
if any(isnan(A_num)) || any(A_num < 0)
%complain, and then "continue" the loop
end
You will find that you can use this right after "a" has been split in to strings.
lbon jbn
lbon jbn 2011-12-1
It still does not accept arrays...
Here is the updated code:
function [ volume ] = cube( A )
p=imread('cubepict.png');
imshow(p)
repeat = 1;
while repeat ==1
a = input ('Please enter a value or values for a according to the diagram: ', 's');
a = regexp(a, ' ', 'split');
A_num = str2double(a);
if any(~isnan(A_num)) || any(A_num < 0)
disp ('Input(s) must be numerical and cannot contain negative values. Please try again.')
else repeat=0;
end
end
fid=fopen('cubelength.txt','w') ;
fprintf(fid, '%1.2i',A_num);
fclose(fid);
load cubelength.txt;
volume = A_num.^3;
fid2=fopen('volume.txt','w');
fprintf(fid2,'%1.2i',volume);
end

请先登录,再进行评论。


lbon jbn
lbon jbn 2011-12-1
I was checking it out again and it seems like it still does not recognize the input are a numerical response and therefore gives the error message I programmed into it. Some how the conversion is failing.

lbon jbn
lbon jbn 2011-12-1
YESS!!! I think i got it, all I had to do was use str2num command rather than str2double as that only works for scalars.
In any case, thanks for you help walter!

类别

Help CenterFile Exchange 中查找有关 AI for Signals 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by