How do I stop random numbers from popping up when I run my code?

2 visualizaciones (últimos 30 días)
Player(1).Name = 'Ivan Barrocio';
Player(1).Position = 'Point Guard';
Player(1).Height = '72';
Player(1).shirt = '22';
Player(2).Name = 'Aries Vega';
Player(2).Position = 'Shooting Guard';
Player(2).Height = '69';
player(2).shirt = '1';
Player(3).Name = 'James Webster';
Player(3).Position = 'Small Forward';
Player(3).Height = '75';
Player(3).shirt = '23';
Player(4).Name = 'Anthony Juarez';
Player(4).Position = 'Power Forward';
Player(4).Height = '76';
Player(4).shirt = '24';
Player(5).Name = 'AJ Asberry';
Player(5).Position = 'Center';
Player(5).Height = '77';
Player(5).shirt = '4';
fprintf(' Name Position Height Shirt \n')
for n=1:5
fprintf('\t%s %s \t%g \t %g\n',Player(n).Name,Player(n).Position,Player(n).Height,Player(n).shirt)
end
I get----> Name Position Height Shirt
Ivan Barrocio Point Guard 55 50
22 Aries Vega Shooting Guard 54 57
James Webster Small Forward 55 53
23 Anthony Juarez Power Forward 55 54
24 AJ Asberry Center 55 55
4 >>
Why are the 55's popping up?

Respuesta aceptada

Stephen23
Stephen23 el 21 de Nov. de 2022
Editada: Stephen23 el 21 de Nov. de 2022
"Why are the 55's popping up?"
Because you are storing all of the numeric data as character, which you then supply to FPRINTF and try to convert using format string '%g' which is not suitable for text... and so FPRINTF prints the character code:
fprintf('%g\n','7')
55
There is nothing random happening here, FPRINTF is is doing the best it can with the inputs you are giving it.
The simple solution is to store numeric data as numeric, not as text like you are doing:
Player(1).Name = 'Ivan Barrocio';
Player(1).Position = 'Point Guard';
Player(1).Height = 72;
Player(1).Shirt = 22;
Player(2).Name = 'Aries Vega';
Player(2).Position = 'Shooting Guard';
Player(2).Height = 69;
Player(2).Shirt = 1;
Player(3).Name = 'James Webster';
Player(3).Position = 'Small Forward';
Player(3).Height = 75;
Player(3).Shirt = 23;
Player(4).Name = 'Anthony Juarez';
Player(4).Position = 'Power Forward';
Player(4).Height = 76;
Player(4).Shirt = 24;
Player(5).Name = 'AJ Asberry';
Player(5).Position = 'Center';
Player(5).Height = 77;
Player(5).Shirt = 4;
for k = 1:numel(Player)
fprintf('%24s %24s %8g %8g\n', Player(k).Name, Player(k).Position, Player(k).Height, Player(k).Shirt)
end
Ivan Barrocio Point Guard 72 22 Aries Vega Shooting Guard 69 1 James Webster Small Forward 75 23 Anthony Juarez Power Forward 76 24 AJ Asberry Center 77 4

Más respuestas (0)

Categorías

Más información sobre Histograms en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by