Can someone help me clean this up, I am sure there is a faster more effiecent way to define value units. Here is my code
u=symunit;
Modulus_Dim=input('\nWhat is the Units for your modulus inputs (0-PSI,1-GPa, 2-MPa, 3-Pa) *Must be consistent* :');
if (Modulus_Dim==0)%Running through an if statement to get units
if true
% code
end
Er=input('\nWhat is Er :')*u.psi;
Em=input('\nWhat is Em :')*u.psi;
Gr=input('\nWhat is Gr :')*u.psi;
Gm=input('\nWhat is Gm :')*u.psi;
else if (Modulus_Dim==1)
Er=input('\nWhat is Er :')*u.GPa;
Em=input('\nWhat is Em :')*u.GPa;
Gr=input('\nWhat is Gr :')*u.GPa;
Gm=input('\nWhat is Gm :')*u.GPa;
else if (Modulus_Dim==2)
Er=input('\nWhat is Er :')*u.MPa;
Em=input('\nWhat is Em :')*u.MPa;
Gr=input('\nWhat is Gr :')*u.MPa;
Gm=input('\nWhat is Gm :')*u.MPa;
else
Er=input('\nWhat is Er :')*u.Pascal;
Em=input('\nWhat is Em :')*u.Pascal;
Gr=input('\nWhat is Gr :')*u.Pascal;
Gm=input('\nWhat is Gm :')*u.Pascal;
end
end

 Accepted Answer

Stephen23
Stephen23 on 5 Feb 2018
Edited: Stephen23 on 5 Feb 2018
Something like this should get you started:
typ = {'psi','GPa','MPa','Pascal'};
csl = sprintf(',%s',typ{:});
pmt = sprintf('What units for your modulus inputs (%s): ',csl(2:end));
unit = input(pmt,'s');
assert(ismember(unit,typ),'Input units must be one of %s',csl(2:end))
Er = str2double(input('What is Er :','s'))*symunit(unit);
Em = str2double(input('What is Em :','s'))*symunit(unit);
Gr = str2double(input('What is Gr :','s'))*symunit(unit);
Gm = str2double(input('What is Gm :','s'))*symunit(unit);
Note how I avoided the bulky if chain by simply calling symunit with the unit string. Also, in general you should use the 's' option for input.

5 Comments

ok, I'm working through the documentation center trying to understand this all, is the ',%s' a variable of somekind?. So for the 's' option your talking about I was under the impression that means store it at face value, but I need MatLab to understand that it is a unit so for later coding it can do unit evaulation.
Stephen23
Stephen23 on 6 Feb 2018
Edited: Stephen23 on 6 Feb 2018
"is the ',%s' a variable of somekind?"
Not according to the usual MATLAB definition of variable: ',%s' is actually an example of a format string used in sprintf (and fprintf too) and it tells sprintf how to convert and format sprintf's input values in the output string/char vector. In this case it converts those names in the cell array to this char vector: ',psi,GPa,MPa,Pascal', which is then used in both the prompt and the error message.
"So for the 's' option your talking about I was under the impression that means store it at face value,"
If you mean that it does not evaluate the input but merely returns it as a char vector, then yes. Evaluating any random input that the user might enter here is highly insecure and invites bugs into your code, thus I used the 's' option to return a string followed by str2double to convert this to a numeric value.
"but I need MatLab to understand that it is a unit so for later coding it can do unit evaulation."
I don't see what that has to do with input evaluating its input or not. I showed you how you can simply supply the (user entered) char vector directly to symunit, thus creating the unit that was requested by the user. I also added an assert to ensure that they enter a valid string. What more do you want?
Kevin Edlebeck
Kevin Edlebeck on 6 Feb 2018
Edited: Kevin Edlebeck on 6 Feb 2018
Sorry, I was confused about the unit part. So its a better practice to take an input, store it as a character (s) and convert it to a value than just store the input as a value which is what I was doing?
Stephen23
Stephen23 on 6 Feb 2018
Edited: Stephen23 on 6 Feb 2018
"So your saying its a better practice to take an input and convert it to a value than just store the input?"
If you want to avoid pointless bugs in your code then avoid letting your user input absolutely anything (either intentionally or accidentally) and allowing it get evaluated by input. You can do this very simply by using the 's', which always returns a char vector (which you can process yourself as required).
This helped me so much, thank you.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!