How can I print the result of a function using fprintf? (matrix and string!)

2 visualizaciones (últimos 30 días)
The variable countries is a 27x1 column vector with 3 letter abbreviations (ex. 'CAN'). Results is a 27x4 matrix with values ranging from 0-40 ish. Why can't I print them together or row by row?
I want the result to look like this:
Country Gold Silver Bronze
CAN 2 1 3
ITA 3 1 4
... etc.
My code:
Please skip to the bottom of this code for the part I'm having issues with.
function [] = Untitled6()
load('olympics.mat')
results = zeros(size(countries,1),4);
results = compute_medals(gold,silver,bronze,countries);
print_country_results(countries,results);
end
function results = compute_medals(gold,silver,bronze,countries);
% computes number of gold, silver, bronze medals and
% total medal tally for a given country and a given sport type
% results = zeros(size(countries,1),4);
for i = 1:length(countries)
country = countries(i, :);
goldCount = 0;
silverCount = 0;
bronzeCount = 0;
for j = 1: length(gold)
if country == gold(j, :)
goldCount = goldCount+ 1;
end
if country == silver(j, :)
silverCount = silverCount + 1;
end
if country == bronze(j, :)
bronzeCount = bronzeCount + 1;
end
end
results(i,1) = goldCount;
results(i,2) = silverCount;
results(i,3) = bronzeCount;
results(i,4) = goldCount + silverCount + bronzeCount;
end
results
end
% HERE! Below is the part I'm having issues with! Please Help!
function [ ] = print_country_results(countries,results)
% prints formatted results
fprintf('Country Gold Silver Bronze Total\n')
for i = 1:length(countries) % = 27
fprintf('%s %d \n', countries(i, :), results (i,:));
end
end
Can I print a matrix & string row by row? Please help!
EDIT: We are not allowed to use a table. :(
  2 comentarios
James Tursa
James Tursa el 18 de Mzo. de 2020
Editada: James Tursa el 18 de Mzo. de 2020
What is the variable 'countries'? I.e., class. Same question for gold, silver, bronze.
Ashley Hayden
Ashley Hayden el 18 de Mzo. de 2020
Editada: Ashley Hayden el 18 de Mzo. de 2020
Countries (27x3) is a char class. Gold, silver, and bronze are also char class. I have attached the file with all the starting variables (olympics.mat). They look like this:
'AUS'
'AUT'
'BLR'
'CAN'
'CHN'
'CRO'
'CZE'
'EST'
'FIN'
'FRA'
'GBR'
'GER'
'ITA'
'JPN'
'KAZ'
'KOR'
'LAT'
'NED'
'NOR'
'POL'
'RUS'
'SLO'
'SUI'
'SVK'
'SWE'
'USA'
'XXX'

Iniciar sesión para comentar.

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 18 de Mzo. de 2020
Editada: Ameer Hamza el 18 de Mzo. de 2020
You can change your function like this
function [ ] = print_country_results(countries,results)
% prints formatted results
fprintf('Country\tGold\tSilver\tBronze\tTotal\n')
for i = 1:length(countries) % = 27
fprintf('%s\t%d\t%d\t%d\t%d\n', countries(i, :), results (i,:));
end
end
Output:
Country Gold Silver Bronze Total
AUS 2 1 0 3
AUT 4 6 6 16
BLR 1 1 1 3
CAN 14 7 5 26
CHN 5 2 4 11
CRO 0 2 1 3
CZE 2 0 4 6
EST 0 1 0 1
FIN 0 1 4 5
FRA 2 3 6 11
GBR 1 0 0 1
GER 10 13 7 30
ITA 1 1 3 5
JPN 0 3 2 5
KAZ 0 1 0 1
KOR 6 6 2 14
LAT 0 2 0 2
NED 4 1 3 8
NOR 9 8 6 23
POL 1 3 2 6
RUS 3 5 7 15
SLO 0 2 1 3
SUI 6 0 3 9
SVK 1 1 1 3
SWE 5 2 4 11
USA 9 15 13 37
XXX 1 0 2 3

Más respuestas (2)

James Tursa
James Tursa el 18 de Mzo. de 2020
Editada: James Tursa el 18 de Mzo. de 2020
When comparing strings, it is not a good idea to use the == operator, which is an element-wise operator. Instead, use a string comparison function or a general comparison function. E.g., instead of this
if country == gold(j, :)
do something like this
if isequal( country, gold(j, :) )
or maybe
if strcmpi( country, gold(j, :) )
Also, in your print statement you need to print four numerical values, not one. E.g. this
fprintf('%s %d \n', countries(i, :), results (i,:));
should be something like this instead
fprintf('%s %7d %6d %6d %5d \n', countries(i, :), results (i,:));

Adam Danz
Adam Danz el 18 de Mzo. de 2020
Put the data into a table and display the table. There are lots of way to create a table. Here's one way,
data = [2 1 3; 3 1 4];
T = array2table(data, 'VariableNames', {'Gold','Silver','Bronze'}, 'RowNames', {'CAN','ITA'});
To display it,
disp(T)
  1 comentario
Ashley Hayden
Ashley Hayden el 18 de Mzo. de 2020
I should've mentioned it in the question itself - I'm not allowed to use a table. Is there a way I can use fprintf?

Iniciar sesión para comentar.

Categorías

Más información sobre Cell Arrays 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