Is it possible to have a user input of any unit? If so, how do you do so?

4 vues (au cours des 30 derniers jours)
Caedon Berkenmeier
Caedon Berkenmeier le 1 Sep 2021
Is it possible to have a user input of any unit? If so, how do you do so?

Réponses (2)

Walter Roberson
Walter Roberson le 1 Sep 2021
You can use an of the usual text input methods to permit the user to enter any unit they want.
The problem is that the conversion tables to match other units are unllkely to be available.
For example, what is the conversion between "jinkees" and "fortnights per spork" ? Your tables are unlikely to know that a jinkee is derived from the knik-jiffy and that the knik is the inverse spork (1/sporks). The jiffy is the time it takes light to travel one fermi, and a fermi is one femtometre. A fortnight is two weeks.
If you do not permit the user to enter jinkees as a unit, then you have violated the premise that the user is to be permitted to enter any unit.

Image Analyst
Image Analyst le 1 Sep 2021
Not sure what that means but take a look at input(), inputdlg(), listdlg(), menu(), etc.
For an example:
% Ask user for two floating point numbers.
defaultValue = {'45.67', '78.91'};
titleBar = 'Enter values';
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 usersValue1 for validity.
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
% Check usersValue2 for validity.
if isnan(usersValue2)
% 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 usersValue2.
usersValue2 = str2double(defaultValue{2});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue2);
uiwait(warndlg(message));
end

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by