fprintf not displaying outside of a function.

2 views (last 30 days)
The Background:
I'm in an engineering computing course in university and there are no lab periods between now and the date on which this assignment is due, so this is my last resort. I have attempt to compile all the information I have found on similar question on both this site and stackexchange, but with no success. I'd like help with this specific problem, not for the solution to be handed to me.
The Question:
Using one function, two sub-functions, a third sub-function that is called upon thrice, and no tables, display: the results for 2010 Olympics, the country with the most medals, the country with most gold metals, and all countries with at least 20 medals. Ties must be accounted for.
The Issue:
I have managed to do everything but the formatting of the functions, I thought I had managed format it correctly but I get nothing but errors regarding static workspaces when loading data from a m file and improperly nested functions (the latter referring to the third sub-function).
The Synopsis/tl;dr:
I could use guidance in the formatting of functions when 'fprintf'ing as I have not been able to get it to display the string in the command window.
The Code:
In the preview, some lines appear to be oddly formatted, sorry.
function[]=Assign4_1448721()
clear,clc; load('olympics2010.mat') %loads countries(27x3, string), gold(27x1), silver(27x1), bronze(27x1)
end
function [medals]=calculate_medals()
for i=[1:27] medalsbronze(i)=length(strmatch(countries(i,:), bronze)); end medalsb=(medalsbronze(1:27))';
for j=[1:27]
medalssilver(j)=length(strmatch(countries(j,:), silver));
end
medalss=(medalssilver(1:27))';
for k=[1:27]
medalsgold(k)=length(strmatch(countries(k,:), gold));
end
medalsg=(medalsgold(1:27))';
medals=[medalsg medalss medalsb];
end
function []=print_medals(medals)
fprintf('Country Gold Silver Bronze Total\n')
for l=[1:26]
fprintf(' %s', countries(l,:))
fprintf(' %2.0f', medals(l,1))
fprintf(' %2.0f', medals(l,2))
fprintf(' %2.0f', medals(l,3))
fprintf(' %2.0f\n', sum(medals(l,:)))
end
end
function []=determine_total_gold_and_at_least_20_medals(medals)
switch(m)
case 1
mostmedals=countries(1,:);
totalmedals=sum(medals(1,:));
for n=[1:25];
if sum(medals((n+1),:))>totalmedals;
totalmedals=sum(medals((n+1),:));
mostmedals=countries((n+1),:);
elseif sum(medals((n+1),:))<totalmedals;
totalmedals=totalmedals;
mostmedals=countries((n),:);
else sum(medals((n+1),:))==totalmedals;
totalmedals=sum(medals((n+1),:));
mostmedals=[mostmedals countries((n+1),:)];
end
end
%I didn't want to make a switch/case for 1-26, so I did 3.
if size(mostmedals)==[1 3];
fprintf('Countries with the most medals: %s\n', mostmedals)
***|Irrelevant code|***
end
case 2
mostgoldmedals=countries(1,:);
totalgoldmedals=medals(1,1);
for o=[1:25];
if medals((o+1),1)>totalgoldmedals;
totalgoldmedals=medals((o+1),1);
mostgoldmedals=countries((o+1),:);
elseif medals((o+1),1)<totalgoldmedals;
totalgoldmedals=totalgoldmedals;
mostgoldmedals=mostgoldmedals;
else medals((o+1),1)==totalgoldmedals;
totalgoldmedals=medals((o+1),1);
mostgoldmedals=[mostgoldmedals countries((o+1),:)];
end
end
%I didn't want to make a switch/case for 1-26, so I did 3.
if size(mostgoldmedals)==[1 3];
fprintf('Countries with the most gold medals: %s\n', mostgoldmedals)
***|Irrelevant code|***
end
case 3
for p=1:27
summedals(p)=sum(medals(p,:));
end
atleast20=find(summedals>=20);
q=length(atleast20);
%I didn't want to make a switch/case for 1-13, so I did 8.
switch q
***|Irrelevant code|***
case 4
fprintf('Countries with at least 20 medals: %s %s %s %s\n', countries((atleast20(:,1)),:), countries((atleast20(:,2)),:), countries((atleast20(:,3)),:), countries((atleast20(:,4)),:))
***|Irrelevant code|***
end
end
for m=[1:3];
determine_total_gold_and_at_least_20_medals(medals)
end
end
Thanks for sticking around!
  2 Comments
Star Strider
Star Strider on 20 Mar 2015
Specifically, what is not working in your code?
‘I get nothing but errors ...’
What are the errors? Please copy and paste the entire text of all error messages (all the red text in the Command Window, including the line that is throwing the error) to a Comment to your original Question. Without that, we have no idea what the problem is.
We’ll do our best to help, but we need specifics.
Image Analyst
Image Analyst on 20 Mar 2015
And you might post olympics2010.mat, if your instructor would be okay with that. We don't like it when people ask us to solve homework without telling us (or even with telling us) and then delete everything later, so if your instructor wouldn't like us giving answers or hints here, let us know. But just a quick scan doesn't show any problems so that's why we'd need to run it ourselves. Or else maybe you can post a screenshot of the output you are getting, like why it's badly formatted.

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!