Problems multiplying two variables

For some reason when my code multiplies two variables it is making the answer much larger than it actually is? For example it calculates 2*5 as being 2650 instead of just 10, I've shown a sample of my code below:
BR = input('Bridge instruments power rating = ', 's');
BT = input('Average time of bridge instruments usage = ', 's');
BE = BR*BT*3600 %Energy used by bridge instruments in kJ
In this example if BR is 5 and BT is 2, it calculates BE as being 9540000
I realise I must be making a simple mistake is it possible for anyone to help?

 采纳的回答

Alfie - look closely at your use of input. You have added the parameter 's' which means that your BR and BT will be strings and not numbers. One way around this is to just do the conversion from string to number using str2double as
BR = str2double(input('Bridge instruments power rating = ', 's'));
BT = str2double(input('Average time of bridge instruments usage = ', 's'));
or, remove the 's'.

更多回答(1 个)

Star Strider
Star Strider 2016-1-13
The problem is 's'. You’re reading in your number as a string, so it appears as the ASCII equivalent. So when you multiply them, you’re multiplying the ASCII values of the characters rather than the number you believe you’re calculating with. Eliminate the 's' and you should be good.

类别

帮助中心File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by