Hey people :) How would i write a Matlab script that asks a group of up to 40 members to enter their height (in cm) and their weight (in kg)?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
i have attempted the question but i haven't made any progress.
My approach was to use a for loop which looped through entries of prompts for the height and weight however i couldn't get the array to be displayed at the end.
Could somebody please enter a solution script as there answer Thanks :)
0 Kommentare
Antworten (2)
Thorsten
am 7 Dez. 2015
N = 40;
for i = 1:N
height(i) = input('Please enter your height (in cm) >> ');
weight(i) = input('Please enter your weight (in kg) >> ');
end
0 Kommentare
Image Analyst
am 7 Dez. 2015
That approach is fine. I'm assuming you got your approach to work (or use Thorsten's method if you don't).
Another option is to use inputdlg() to ask for both height and weight in the same dialog box
% Ask user for two floating point numbers.
defaultValue = {'45.67', '78.91'};
titleBar = 'Enter a value';
userPrompt = {'Enter height : ', 'Enter weight: '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Convert to floating point from string.
height= str2double(caUserInput{1})
weight= str2double(caUserInput{2})
% Check for a valid number.
if isnan(height)
% 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 height.
height = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
if isnan(weight)
% 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 weight.
weight = str2double(defaultValue{2});
message = sprintf('I said it had to be a number.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
To display it after the loop, you can simply put the names of the arrays with no semicolon on their own line:
height
weight
0 Kommentare
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!