How to Validate User Input in MATLAB?

In summary: This is the autograder remarks:Problem 4: size(uiVal) is not equal to [1,4]*the value of errorCode is incorrect for the variables: uiVal = '[1 2 3 4]; The size of your variable is not as expected*the value of errorCode is incorrect for the variables: uiVal = [1 2 3 4 5]; The size of your variable is not as expected*the value of errorCode is incorrect for the variables: uiVal = @sign; The size of your variable is not as expected
  • #1
gfd43tg
Gold Member
950
50

Homework Statement



Often times, a program accepts input from a user, and needs to check the validity of the input, and
then produce useful and informative error messages if the input is invalid. Suppose that uiVal is a
variable representing the user's input, and that errorCode is an 1-by-0 empty array. Write Matlab
code that appends to errorCode various identifying integers depending on certain conditions as
follows:

1, if uiVal is not a numeric, real-valued array
2, if uiVal is not all integer valued. Note that if uiVal is not numeric, real-valued (as is
checked in the rst item) then this condition should not be checked, since it may produce
an error.
3, if any values of uiVal are less than 1 or greater than 6. Note that if uiVal is not numeric,
real-valued (as is checked in the rst item) then this condition should not be checked, since
it may produce an error.
4, if uiVal is not 2-dimensional (use command ndims) or it's size is not equal to 1-by-4.

Homework Equations


The Attempt at a Solution


Right now my code is getting 4/7, I can't figure out what is wrong. Everything seems fine in the code, but when I run uiVal = @sign it gives errorCode = [1 1]

Code:
errorCode = zeros(1,0);
if ~(isnumeric(uiVal) && isreal(uiVal))
    errorCode = [errorCode, 1];
else
    if (uiVal ~= round(uiVal))
       errorCode = [errorCode, 2];
    end 
    if (any(uiVal < 1) | any(uiVal > 6))
    errorCode = [errorCode, 3];
    end 
end
    if ndims(uiVal)~= 2 | size(uiVal) ~= [1,4]
    errorCode = [errorCode, 4];
    end

This is the autograder remarks:
Code:
Problem 3: 4/7
*the value of errorCode is incorrect for the variables: uiVal = '[1 2 3 4]'; 
The size of your variable is not as expected
*the value of errorCode is incorrect for the variables: uiVal = [1 2 3 4 5]; 
The size of your variable is not as expected
*the value of errorCode is incorrect for the variables: uiVal = @sign; 
The size of your variable is not as expected
 
Last edited:
Physics news on Phys.org
  • #2
Im not sure what's causing your problem but you need to properly indent your code for clarity.

The if ndims... statement block should be outdented tight?

I ran it on Freemat and it seemed to work so here's the console printout. I didn't try all cases:

Code:
 FreeMat v4.0
 Copyright (c) 2002-2008 by Samit Basu
 Licensed under the GNU Public License (GPL)
 Type <help license> to find out more
      <helpwin> for online help
      <pathtool> to set or change your path
 Use <dbauto on/off> to control stop-on-error behavior
 Use ctrl-b to stop execution of a function/script
 Use <rootpath gui> to set/change where the FreeMat toolbox is installed
--> errorCode = zeros(1,0);
if ~(isnumeric(uiVal) && isreal(uiVal))
    errorCode = [errorCode, 1];
else
    if (uiVal ~= round(uiVal))
       errorCode = [errorCode, 2];
    end
    if (any(uiVal < 1) | any(uiVal > 6))    errorCode = [errorCode, 3];
    end
end
    if ndims(uiVal)~= 2 | size(uiVal) ~= [1,4]
    errorCode = [errorCode, 4];
-->     endError: Undefined function or variable uiVal
-->     end
Error: Undefined function or variable uiVal
--> uiVal=[1,2,3,4]
uiVal =
 1 2 3 4
--> errorCode = zeros(1,0);
if ~(isnumeric(uiVal) && isreal(uiVal))
    errorCode = [errorCode, 1];
else
    if (uiVal ~= round(uiVal))
       errorCode = [errorCode, 2];
    end
    if (any(uiVal < 1) | any(uiVal > 6))    errorCode = [errorCode, 3];
    end
end
    if ndims(uiVal)~= 2 | size(uiVal) ~= [1,4]
    errorCode = [errorCode, 4];
-->     end
--> print errorCode
--> errorCode
ans =
  Empty array 1 0
--> uiVal=[ 12.3, 14, 15.6 16]
uiVal =
 Columns 1 to 3
   12.3000   14.0000   15.6000
 Columns 4 to 4
   16.0000
--> errorCode = zeros(1,0);
if ~(isnumeric(uiVal) && isreal(uiVal))
    errorCode = [errorCode, 1];
else
    if (uiVal ~= round(uiVal))
       errorCode = [errorCode, 2];
    end
    if (any(uiVal < 1) | any(uiVal > 6))    errorCode = [errorCode, 3];
    end
end
    if ndims(uiVal)~= 2 | size(uiVal) ~= [1,4]
    errorCode = [errorCode, 4];
-->     end
--> errorCode
ans =
 2 3
--> uiVal=['a','b','c','d']
uiVal =
abcd
--> errorCode = zeros(1,0);
if ~(isnumeric(uiVal) && isreal(uiVal))
    errorCode = [errorCode, 1];
else
    if (uiVal ~= round(uiVal))
       errorCode = [errorCode, 2];
    end
    if (any(uiVal < 1) | any(uiVal > 6))
    errorCode = [errorCode, 3];
    end
end
    if ndims(uiVal)~= 2 | size(uiVal) ~= [1,4]
    errorCode = [errorCode, 4];
-->     end
--> errorCode
ans =
 1
 
  • #3
The problem was this statement
Code:
if ndims(uiVal)~= 2 | size(uiVal) ~= [1,4]

it was changed to

Code:
if ndims(uiVal)~= 2 | isequal(size(uiVal), [1,4])
 

Related to How to Validate User Input in MATLAB?

1. What is the syntax for conditional operations in MATLAB?

The syntax for conditional operations in MATLAB is:
if (condition)
     statement(s)
elseif (condition)
     statement(s)
else
     statement(s)
end

The if statement is followed by parentheses containing the condition to be evaluated. If the condition is true, the statement(s) within the if block will be executed. If the condition is false, the elseif block will be checked, and if that condition is true, its statement(s) will be executed. If all conditions are false, the else block will be executed.

2. How do I use logical operators in conditional operations in MATLAB?

Logical operators such as and (&&), or (||), and not (~) can be used to combine multiple conditions in a single if statement. For example, if (x > 0 && y < 10) will only execute the statement(s) if both conditions are true. Parentheses can also be used to group conditions together.

3. Can I have nested conditional statements in MATLAB?

Yes, it is possible to have nested conditional statements in MATLAB. This means that an if or elseif block can contain another if statement, creating a hierarchy of conditions. However, it is important to use proper indentation and syntax to avoid errors and make the code more readable.

4. How do I use conditional operations to execute different code based on user input?

You can use the input() function to prompt the user for input and store it in a variable. Then, you can use conditional operations to check the value of the variable and execute different code based on the user's input. For example, if (input == 'yes') will only execute the statement(s) if the user enters 'yes' as their input.

5. Can I use conditional operations to control the flow of a loop in MATLAB?

Yes, conditional operations can be used within a loop to control its flow. For example, you can use an if statement to check a condition at each iteration of a for or while loop, and use the break statement to exit the loop if the condition is met. This can be useful for creating more dynamic and efficient loops.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
961
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
965
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
Back
Top