I am trying to disallow the user from inputting non-sense answers.

조회 수: 4 (최근 30일)
Bhavjeet Chatha
Bhavjeet Chatha 2023년 2월 9일
답변: Steven Lord 2023년 2월 9일
I am trying to disallow the user from inputting non-sense or non-numeric answers. I have tried functions like isnumeric == 0, but no luck so far. If someone could please determine what I need to do differently, and make sure it works before submitting an answer. I have had many suggestions thus far that do not work.
% prompt the user to input a square matrix
userMatrix = input('Please input a square matrix: ');
if isnumeric(userMatrix) == 0
error('not a numeric value')
% if input is empty, give an error message
elseif isempty(userMatrix)
error('You have entered nothing, program is ending');
% if input isn't a square matrix, give an error message
else
[x, y] = size(userMatrix)
if x ~= y
error('You have entered a non-square matrix, program is ending');
% check to see if input is a scalar
elseif y == 1
disp('You have entered a scalar value: ')
disp(userMatrix)
% show the user their input and the size of the matrix
else
disp(['You have entered a matrix of size ', num2str(x), ' by ', num2str(y) ])
disp('This is what your matrix looks like: ')
disp(userMatrix)
end
end

답변 (3개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 2월 9일
You can try using ischar(), e.g.:
A = input('Enter square matrix: ');
if ischar(A)
error('Wrong entries')
else
disp(A)
end

Dinesh
Dinesh 2023년 2월 9일
Hi Bhavjeet,
The code that you have provided works as expected. Functions like isnumeric() and isempty() work correctly.
% prompt the user to input a square matrix
userMatrix = input('Please input a square matrix: ');
if isnumeric(userMatrix) == 0
error('not a numeric value');
% if input is empty, give an error message
elseif isempty(userMatrix)
error('You have entered nothing, program is ending');
% if input isn't a square matrix, give an error message
else
[x, y] = size(userMatrix);
if x ~= y
error('You have entered a non-square matrix, program is ending');
% check to see if input is a scalar
elseif y == 1
disp('You have entered a scalar value: ');
disp(userMatrix);
% show the user their input and the size of the matrix
else
disp(['You have entered a matrix of size ', num2str(x), ' by ', num2str(y) ]);
disp('This is what your matrix looks like: ');
disp(userMatrix);
end
end
I believe that there could be a mistake while entering the input.
The following are some output examples in the code provided by you.
Case 1:
Case 2:
Case 3:
Case 4:
Case 5:
All these cases work as expected depending on the input from the user.

Steven Lord
Steven Lord 2023년 2월 9일
You are missing at least one check. In addition, since the code that gets executed if any of your if statements are satisfied throws an error you don't need an elseif "chain". You can just use separate if / end blocks. I would also use the not operator ~ instead of comparing the result of the is* check to 0.
if ~isnumeric(userMatrix)
error('not a numeric value')
end
% if input is empty, give an error message
if isempty(userMatrix)
error('You have entered nothing, program is ending');
% if input isn't a square matrix, give an error message
end
Here's where you have a missing check. Since you want to ensure your input is a numeric matrix check that it's a matrix using ismatrix. This will eliminate N-dimensional arrays with N > 2.
if ~ismatrix(userMatrix)
error('N-dimensional arrays with N > 2 are not allowed')
end
This avoids a bug in your next code. When you call size on an array with fewer outputs than the input has dimensions, the last output is the product of the sizes in the trailing dimensions. In the example below A has 3 dimensions but size has 2 outputs so the second output is the product of the sizes of A in the second and third dimensions.
>> A = zeros(4, 2, 2);
>> [rows, cols] = size(A)
rows =
4
cols =
4
So you want to either call size with the dimension input (to explicitly ask for the number of rows and columns) or make sure your input only has 2 dimensions before calling size with only two outputs.
>> [rows, cols] = size(A, [1 2])
rows =
4
cols =
2
I'd also use more descriptive variable names to make the code more self-explanatory. x and y could mean anything; nRows and nCols suggest number of rows and number of columns. You might even want to use numRows and numCols to make that suggestion stronger to readers of your code.
[nRows, nCols] = size(userMatrix);
if nRows ~= nCols
error('You have entered a non-square matrix, program is ending');
end
% check to see if input is a scalar
elseif y == 1
disp('You have entered a scalar value: ')
disp(userMatrix)
% show the user their input and the size of the matrix
else
While you could check for scalar-ness this way (since we know if it's square and has only 1 column it must also have 1 row) I'd use isscalar (again so the code is self-explanatory.)
if isscalar(userMatrix)
% handle a scalar userMatrix
end
If you replace the code from "elseif y == 1" to the following "else" inclusive, the code below will be outside any if / elseif / else / end block.
disp(['You have entered a matrix of size ', num2str(x), ' by ', num2str(y) ])
I'd personally use string for this, avoiding the need for num2str. Let the + operator for strings handle converting the integer values nRows and nCols into their text representations.
disp("You have entered a matrix of size " + nRows + " by " + nCols)
disp('This is what your matrix looks like: ')
disp(userMatrix)
end
end
One final suggestion: if you were performing these checks not as part of an assignment that requires you to use input but in your own function, consider using an arguments block to enforce these requirements.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by