How do I add the command lower() to my code? Also I'm supposed to use sort() but I don't know where or what for.

1 view (last 30 days)
This is my code:
disp('Welcome to the HONDA Price Statistics Report!');
disp(' ');
company = input('Enter the name of the company: ','s');
model = input('Enter the Model and Trim level: ','s');
correct = false;
while~correct
price = input('Enter the input vector inside brackets of at least 6 values greater than 15,000[]: ');
if length(price) < 6
disp('You must enter at least 6 values all of which must be greater than $15,000');
else
if min(price) <= 15000
disp('You must enter at least 6 values all of which must be greater than $15,000');
else
correct = true;
end
end
end
stat1 = input('Enter the name of the first statistic to calculate: ','s');
stat2 = input('Enter the name of the second statistic to calculate: ','s');
stat3 = input('Enter the name of the third statistic to calculate: ','s');
[res1,res2,res3] = HONDA_Statistics(stat1, stat2, stat3, price);
fprintf('\nCompany Name: %s\n', company);
fprintf('Model Name and The Trim Level: %s\n', model);
fprintf('The %s is: %5.2f\n', stat1, res1);
fprintf('The %s is: %5.2f\n', stat2, res2);
fprintf('The %s is: %5.2f\n', stat3, res3);
disp('Goodbye!');
And this is my user-made function:
function [res1,res2,res3] = HONDA_Statistics(stat1, stat2, stat3, price)
res1 = [];
res2 = [];
res3 = [];
if isequal('mean', stat1)
res1 = mean(price);
else
if isequal('std',stat1)
res1 = std(price);
else
if isequal('median', stat1)
res1 = median(price);
else
disp('Input Invalid')
end
end
end
if isequal('mean', stat2)
res2 = mean(price);
else
if isequal('std',stat2)
res2 = std(price);
else
if isequal('median', stat2)
res2 = median(price);
else
disp('Input Invalid')
end
end
end
if isequal('mean', stat3)
res3 = mean(price);
else
if isequal('std',stat3)
res3 = std(price);
else
if isequal('median', stat3)
res3 = median(price);
else
disp('Input Invalid')
end
end
end

Answers (1)

Dyuman Joshi
Dyuman Joshi on 6 Mar 2022
Use lower() to convert all the input from the user to lowercase. User may input - 'Mean', 'MEAN', etc etc. Instead of checking all options convert them to lowercase and proceed further.
sort() was probably asked to use to get the minimum value but you used min()
Also, switch case would be a better option here than to use multiple if statements.
switch lower(stat1)
case 'mean'
res1=mean(price);
case 'std'
res1=std(price);
case 'median'
res1=median(price);
otherwise
disp('Input Invalid')
end
Extra tip - If you can use varargin, you can make it a loop instead of doing it 3 times.
  5 Comments
Belle Dionido
Belle Dionido on 7 Mar 2022
So that didn’t work for me, I put lower(stat1); after the stat 1 input in my code. So:
stat1 = input('Enter the name of the first statistic to calculate: ','s'); lower(stat1);
Dyuman Joshi
Dyuman Joshi on 7 Mar 2022
Try this -
stat1 = input('Enter the name of the first statistic to calculate: ','s');
Unable to run the 'fevalJSON' function because it calls the 'input' function, which is not supported for this product offering.
if isequal(lower(stat1),'mean')
res1=mean(price);
elseif isequal(lower(stat1),'std')
res1=std(price);
elseif isequal(lower(stat1),'median')
res1=median(price);
else disp('Input Invalid');
end

Sign in to comment.

Categories

Find more on Descriptive Statistics in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!