sending an error message to the user

I am trying to write a code where the user is asked to input a value however the value must be (0<x<=0.25)how can I send the user a error message if they do not enter a value within the limits and allow them to attempt another input?

Antworten (2)

KSSV
KSSV am 28 Nov. 2016

0 Stimmen

function Hello(x)
if ~(x >0 && x <= 0.25)
error('input is not 0<x<=0.25')
end

3 Kommentare

Sarah Chappell-Smith
Sarah Chappell-Smith am 28 Nov. 2016
Bearbeitet: KSSV am 29 Nov. 2016
Hi I am still having a problem, the code keeps saying that there is an error and can not run. I'm sure I have entered something in correctly but am struggling to get the code to work.
clc
clear all
x=input('Enter the dimension of the x axis of the plate: ');
y=input('Enter the dimension of the y axid of the plate: ');
Fo=input('Enter a value for the Fourier Number: ')
function Hello(x)
if ~(x >0 && x <= 0.25)
error('input is not 0<x<=0.25')
end
end
KSSV
KSSV am 29 Nov. 2016
How you want to use the function? In the above no where you have called the function.
I've managed to get it to work using;
while f0 <= 0 f0>0.25
f0=input('Enter a value for the Fourier Number: ');
end

Melden Sie sich an, um zu kommentieren.

Image Analyst
Image Analyst am 4 Dez. 2016

0 Stimmen

Use errordlg() or helpdlg() wrapped inside a uiwait():
Try this snippet:
% Ask user for two floating point numbers.
defaultValue = {'45.67', '78.91'};
titleBar = 'Enter a value';
userPrompt = {'Enter floating point number 1 : ', 'Enter floating point number 2: '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Convert to floating point from string.
usersValue1 = str2double(caUserInput{1})
usersValue2 = str2double(caUserInput{2})
% Check for a valid number.
if isnan(usersValue1)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
usersValue1 = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
% Do the same for usersValue2

Kategorien

Mehr zu Simulink finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 28 Nov. 2016

Beantwortet:

am 4 Dez. 2016

Community Treasure Hunt

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

Start Hunting!

Translated by