How to make error checking for each element in array?

6 visualizaciones (últimos 30 días)
lbon jbn
lbon jbn el 30 de Nov. de 2011
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

Respuestas (3)

Walter Roberson
Walter Roberson el 30 de Nov. de 2011
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 comentarios
Walter Roberson
Walter Roberson el 1 de Dic. de 2011
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 el 1 de Dic. de 2011
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

Iniciar sesión para comentar.


lbon jbn
lbon jbn el 1 de Dic. de 2011
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 el 1 de Dic. de 2011
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!

Categorías

Más información sobre AI for Signals en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by