user4739218
user4739218

Reputation: 19

How to save multiple inputs in different formats?

Here is my code:

angles = 'Please enter the Euler angles in degrees- yaw(x),pitch(y) and roll(z) \n:';
ea = input(angles,'s');
cos(ea(1))

This code saves the elements of the input string 'ea' as char. How should I save the input in degrees directly? Using cos(ea(1)) gives an error:

Undefined function 'cos' for input arguments of type 'char'. 

Upvotes: 0

Views: 30

Answers (2)

user3667217
user3667217

Reputation: 2192

By doing input(prompt,'s'), you're specifically telling it to return the user input as string. You need to drop the 's' to get numeric input. Also, you need to do input three times to actually get three inputs. Use a structure to store the input in different fields because it can take string as a field name. It's more organized and easily understandable.

prompt = 'Please enter Euler angles in degress, ';
choice = {'yaw(x): ', 'pitch(y): ', 'roll(z): '};

for ii = 1 : numel(choice)
    ea.(choice{ii}(1:end-5)) = input([prompt, choice{ii}]);
end

But of course, this will also work :

for ii = 1 : numel(choice)
    ea(ii) = input([prompt, choice{ii}]);
end

Upvotes: 0

gregswiss
gregswiss

Reputation: 1456

Firstly the input function returns a string, so you will need to convert that to a numerical value. Secondly cos takes input in radians not degrees, so you will either need to convert to radians or to use cosd instead of cos.

angles = 'Please enter the Euler angles in degrees- yaw(x),pitch(y) and roll(z) \n:';
ea = str2double(input(angles,'s'));
cosd(ea(1))

You can also use input without the 's' parameter. In that case input will evaluate the expression passed as a string and return the result. For example in that case not only can you pass singles values such as '90' but you could also pass things like: 3*180/4 as input

angles = 'Please enter the Euler angles in degrees- yaw(x),pitch(y) and roll(z) \n:';
ea = input(angles);
cosd(ea(1))

Upvotes: 1

Related Questions