# Why am I getting 387 as the sum of my vectors when I input 1x1x1?

1 view (last 30 days)
Yousaf Ijaz on 4 Dec 2019
Commented: Yousaf Ijaz on 4 Dec 2019
i probaly dont understand how the str2num command wroks but sor some reason when i input 1x1x1, get matlab to remove the x's from it and convert whats left to a serires of numbers and than take the sum of those numbers i get 387. why is that? i know matlab reads a string 1 as 49 thats probably why im getting that answer. How would i get aorund it and get Matlab to read string 1 as the value 1? Thanks for the help.
numbers = input('Enter your numbers that you want the sum of serperated by x: ', 's')
erase(numbers, 'x')
str2num(numbers)
sum1 = sum(numbers,'all')
fprintf('the sum of your numbers is = %f', sum1)

Guillaume on 4 Dec 2019
Edited: Guillaume on 4 Dec 2019
Your problem is not with str2num (although using str2num is not recommended, but that's another topic).
Your problem is more fundamental is that the only thing that
somefunc(whateverinput)
does is display the output(s) of the function to the command line and then immediately discards the output(s) since it's not stored in anything.
somevariable = somefunc(whateverinput);
on the other hand does store that output in somevariable.
In effect, your call to erase does nothing since you never store ther result of erasing 'x' and your call to str2num does nothing since you never store its output either. Therefore you're summing the character of your numbers variable which still contains your original input '1x1x1'.

#### 1 Comment

Yousaf Ijaz on 4 Dec 2019
thank you that helped solve the issue. I thought matlab would keep that value until the end of the code.