Converting a printed set of data to a structure or structure array

1 view (last 30 days)
I have this code to display the medal standings (gold, silver, bronze, and total) for each country and need to save the information into a structure or structure array and am looking for some guidance on how to achieve this.
function [] = Assign4_123456()
% Clear Workspace and Command Window and load olympics.mat
clear; clc;
load('olympics.mat','gold', 'silver', 'bronze', 'countries');
% function calls
[gold_medals, silver_medals, bronze_medals, total_medals] = compute_medals(gold, silver, bronze, countries);
print_country_results(gold_medals, silver_medals, bronze_medals, total_medals, countries);
print_best_countries(gold_medals, total_medals, countries, 1);
print_best_countries(gold_medals, total_medals, countries, 2);
print_best_countries(gold_medals, total_medals, countries, 3);
end
% compute medals for each country using a sub-function that is
% called using a loop over all countries
function [gold_medals, silver_medals, bronze_medals, total_medals] = 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
% initialize variables
total_countries = length(countries);
gold_medals = zeros(total_countries, 1);
silver_medals = zeros(total_countries, 1);
bronze_medals = zeros(total_countries, 1);
total_medals = zeros(total_countries, 1);
for i = 1:total_countries
country = countries(i, :);
gold_total = 0;
silver_total = 0;
bronze_total = 0;
for j = 1: length(gold)
if country == gold(j, :)
gold_total = gold_total + 1;
end
if country == silver(j, :)
silver_total = silver_total + 1;
end
if country == bronze(j, :)
bronze_total = bronze_total + 1;
end
gold_medals(i) = gold_total;
silver_medals(i) = silver_total;
bronze_medals(i) = bronze_total;
total_medals(i) = gold_total+silver_total+bronze_total;
end
end
end
% display medal counts for all countries
function [] = print_country_results(gold_medals, silver_medals, bronze_medals, total_medals, countries)
% prints formatted results
% display the gold, silver and bronze medal count for each country
fprintf('Country Gold Silver Bronze Total\n');
for i = 1:length(countries)
if strcmp(countries(i, :), 'XXX') == 0
fprintf('%7s %4d %6d %6d %5d\n', countries(i, :), gold_medals(i), silver_medals(i), bronze_medals(i), total_medals(i));
end
end
end
  2 Comments
Stephen23
Stephen23 on 14 Mar 2019
"Converting a printed set of data to a structure or structure array and saving it to a file."
There is no easy way to do this: printing is an output operation, and data that has been printed to the command window is not intended as an input to other operations. By far the easiest way to generate a structure array would be from your existing data arrays.
Emily
Emily on 15 Mar 2019
Can you please point me in the direction on how to approach doing this?

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 14 Mar 2019
The simplest solution that satisfies your description:
S = load('olympics.mat','gold', 'silver', 'bronze', 'countries');
  2 Comments
Emily
Emily on 15 Mar 2019
That's what I had thought but I tried it already and it gave me the error:
Undefined function or variable 'gold'.
Error in Assign4B_1592574 (line 23)
[gold_medals, silver_medals, bronze_medals, total_medals] = compute_medals(gold, silver, bronze,
countries);
so I'm not sure if there's something I am missing or don't fully understand.
Stephen23
Stephen23 on 15 Mar 2019
"That's what I had thought but I tried it already and it gave me the error:"
Undefined function or variable 'gold'
Of course: if you import that data into a structure then the data will be in that structure, not in the variables named gold, etc.
You asked "...need to save the information into a structure or structure array..." and that is what my answer shows you: it stores the file data in a structure. If you want to store some other data in a structure then you need to decide exactly which data you want, and then simply add it to a structure:
T.A = someDataArray
T.B = moreDataArray
etc.

Sign in to comment.

Categories

Find more on Structures 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!