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

조회 수: 2 (최근 30일)
Emily
Emily 2019년 3월 14일
댓글: Stephen23 2019년 3월 15일
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
Stephen23
Stephen23 2019년 3월 14일
"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 2019년 3월 15일
Can you please point me in the direction on how to approach doing this?

댓글을 달려면 로그인하십시오.

답변 (1개)

Stephen23
Stephen23 2019년 3월 14일
The simplest solution that satisfies your description:
S = load('olympics.mat','gold', 'silver', 'bronze', 'countries');
  댓글 수: 2
Emily
Emily 2019년 3월 15일
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 2019년 3월 15일
"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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by