Why is my sum outputting a curly bracket?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Olivia Colombo
am 4 Feb. 2019
Kommentiert: madhan ravi
am 4 Feb. 2019
I'm trying to output even numbers 2-200 in one single line of code, but instead of displaying the number it's displaying a curly bracket after the text.
Here's the code:
disp(['Sum of even numbers from 2 to 200 =',sum(2:2:200)])
0 Kommentare
Akzeptierte Antwort
Star Strider
am 4 Feb. 2019
You are attempting to combine strings and numeric data. That will not work.
Try this:
disp(['Sum of even numbers from 2 to 200 =',num2str(sum(2:2:200))])
The best option is likely sprintf or fprintf.
0 Kommentare
Weitere Antworten (2)
madhan ravi
am 4 Feb. 2019
num2str(sum(2:2:200))
I recommend to use
fprintf('Sum of even numbers from 2 to 200: %d',sum(2:2:200))
1 Kommentar
madhan ravi
am 4 Feb. 2019
Or
"Sum of even numbers from 2 to 200: "+sum(2:2:200)
ronnybongos
am 4 Feb. 2019
are you trying output the sum of even numbers 2-200 (i.e. 10100)? Try:
disp(['Sum of even numbers from 2 to 200 =',num2str(sum(2:2:200))])
otherwise MATLAB converts 10100 to unicode, which is a curly bracket
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!