How to use prompts with User Defined Functions

Help!
So I have been trying to create a user defined function with a prompt as an input so I can data validate a multitude of variables with changing input prompts.
I need it to be silimar to an input function that has one output and one input.
function [Out] = ValidInput('Prompt')
% In is the variable name i want the prompt to have but have no clue to get a value from the prompt.
c = 0;
while In <= 0 & c < 3
% somehow ask again with the same prompt
c = c+1;
end
if In == 0
error('Entered value is zero. Cannot solve problem, program terminated.')
elseif In < 0
warning('Invalid value entered 3 times. Taking the absolute value of the entered value.')
Out = abs(In);
end
end

댓글 수: 1

Can you give an example of how you will call this function, and what will be the expected output after running this function?

댓글을 달려면 로그인하십시오.

답변 (1개)

Sahithi Metpalli
Sahithi Metpalli 2020년 3월 9일

0 개 추천

Hi,
Follow the below code
function [Out] = ValidInput(Prompt)
% In is the variable name i want the prompt to have but have no clue to get a value from the prompt.
In = input(Prompt)
c = 0;
while In <= 0 & c < 3
% somehow ask again with the same prompt
In = input(Prompt)
c = c+1;
disp(c);
end
if In == 0
error('Entered value is zero. Cannot solve problem, program terminated.')
elseif In < 0
warning('Invalid value entered 3 times. Taking the absolute value of the entered value.')
Out = abs(In);
else
%assigning In to output
Out = In;
end
end
Call the function in the following way
out = ValidInput("prompt")

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

질문:

2020년 3월 6일

답변:

2020년 3월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by