Topics: User Defined Functions and MATLAB Conditionals A mathematician needs you
ID: 673563 • Letter: T
Question
Topics: User Defined Functions and MATLAB Conditionals
A mathematician needs you to write a MATLAB Function to verify the format of a three character floating point number input. A valid (3 character) floating point number has the following properties:
1. Consists of exactly three of the following characters: +, -, . (decimal point), and 0 through 9
2. The + or - character may only appear (once) as the first character
3. The . (decimal point) character must appear exactly once, as either the first, second, or third character
4. All other characters must be the 0 through 9 characters
Outline:
1. Create a MATLAB Function .m file
2. Establish its name (validate), input (a String), and output (the logical value true or false)
3. Write the necessary MATLAB commands to determine the correct result, using the given input
4. Return the result, by assigning the output variable
5. Make sure to test your function when it is done – You need to write a separate Script file
Notes(s):
You will need to use the
<var> = input(<prompt>, 's');
version of input to read in and assign <var> the non-evaluated string that the user types.
Your function must not display anything to the screen, nor get any user input.
Your function will simply return true or false based on the validity of its input.
Sample Run(s):
Please enter a valid (3 character) double literal : 1234
1245 is not a valid (3 character) double literal
Please enter a valid (3 character) double literal : 123
123 is not a valid (3 character) double literal
Please enter a valid (3 character) double literal : +45
+45 is not a valid (3 character) double literal
Please enter a valid (3 character) double literal : 4.5
4.5 is a valid (3 character) double literal
Please enter a valid (3 character) double literal : 45.
45. is a valid (3 character) double literal
Please enter a valid (3 character) double literal : .45
.45 is a valid (3 character) double literal
Please enter a valid (3 character) double literal : +.1
+.1 is a valid (3 character) double literal
Please enter a valid (3 character) double literal : -1.
-1. is a valid (3 character) double literal
Please make it simple, and write it as codes..
thank you
Explanation / Answer
main.m
prompt = "Please enter a valid (3 character) double literal ";
str = input(prompt,'s');
bool = check_float(str)
check_float.m
function output = check_float(str)
if size(str) != 3
output = false;
end
if numel(strfind(str,'.')) != 1
output = false;
end
if numel(strfind(str(2:end),'+')) > 0
output = false;
end
if numel(regexp(str,'(+|.|[0-9])(.|[0-9])(.|[0-9])')) == 0
output = false;
end
output = true;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.