How do I stop random numbers from popping up when I run my code?
조회 수: 3 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
Stephen23
2022년 11월 21일
편집: Stephen23
2022년 11월 21일
"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')
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
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!