MTATLAB 数据分析与可视化 Assignment2


某带学双创…
咳咳…都懂的
有帮助点个赞收藏一下啥的…

题目

Assignment Overview
Total marks are 50. Task wise breakdown of marks is provided. The question covers the material studied until Lecture 12.

Regarding “studentMarks.xlxs” data file
The student names, student numbers and marks in this data file were randomly generated and do not represent the performance of any student.  
Question:
Write a Matlab function gradeSummary(excelfilename) that reads the Excel file where the first 3 columns are the given name, last name and student number, and the remaining columns are marks obtained in different subjects out of 100. An example file “studentMarks.xlsx” is provided.
Part (A): Your function should plot a summary of the marks as shown below:
输出示例
Task 1: (6 marks)
The top left subplot must show the average marks in each subject. Label the axes appropriately and give the plot an appropriate title. Rotate the subject names on the x-axis by 45 degrees so that they do not overlap.
Task 2: (6 marks)
The next subplots are 3D pie-charts (with split slices) where each one shows the proportion of students with different grades in a subject as per the grading table below:
Marks range 0 – 49 50 – 59 60 – 69 70 – 79 80 – 100
Grade F P CR D HD
Task 3: (6 marks)
The function must work for any number of subjects/units (from 1 to 5) and for any subject names without giving error and for any number of students.
Task 4: (6 marks)
Some grades may be missing for some subjects/units making the default colours inconsistent between pie-charts. Your function should explicitly colour the pie-chart slices so that the colours are consistent i.e. red, yellow, blue, cyan, green colours for F, P, CR, D, HD grades respectively.
Task 5: (4 marks)
Rather than displaying the figure with default values, the function should specify the figure size and background colour. Use 1280x720 (width x height) as the figure size and [0.9, 0.7, 0.4] for background colour or any other appropriate values (other than default ones) to make the figure more appealing and understandable.
Task 6: (2 marks)
Give appropriate titles to each subplot. Remember that the subject names can change in the Excel file.
NOTE: Your function will be tested with a different Excel file that will have different subject/unit names, different number of subjects (not more than 5) and different number of students. You must ensure that your function works for all such input files. In the Excel file, subject/unit names and marks will always start at column 4 and the marks will always be valid i.e. between 0-100 out of 100.
Part (B): (20 marks)
The same function (i.e. “gradeSummary”) should also return a structure array containing the Excel data. For the given example Excel file “studentMarks.xlsx” the returned structure should be:

1x400 struct array with fields:

Given_Name
Last_Name
Student_Number
Math
CAV
Civil
Python
Electrical

The structure fields for the subjects must specify the grade. The first entry in the structure should be:

    Given_Name: 'Anthony'
     Last_Name: 'De Bavay'
Student_Number: 2380780
          Math: 'F'
           CAV: 'HD'
         Civil: 'CR'
        Python: 'HD'
    Electrical: 'CR'

NOTE: The field names must exactly match those provided in the first row of the Excel file. DO NOT hard code field names since your function will be tested with a different Excel file e.g. the first field may be First_name and the subject/unit names may be different.

代码实现

Matlab版本 2019a

信息读取与录入

Step1 信息读取

%信息读取
[num_info,str_info]=xlsread('StudentMarks');

Step2 信息录入

%信息录入
%统计总个数
stu_num     = size(num_info,1);%学生个数
subject_num = size(num_info,2)-1;%科目个数
%建立学生结构体数组
stu_info      = cell(stu_num,subject_num+3);
%录入学生信息
stu_info(:,1) = str_info(2:stu_num+1,1);
stu_info(:,2) = str_info(2:stu_num+1,2);
stu_info(:,3) = cellstr(num2str(num_info(:,1)));
id           =  num_info(:,1);
%录入科目与各科成绩数据
subjects      = str_info(1,4:subject_num+3);
sub_score     = num_info(1:stu_num,2:subject_num+1);
sub_grade     = cell(stu_num,subject_num);%定义成绩等级数组
average_score = sum(sub_score)/stu_num;%平均分数

PART A

预设背景
figure('color',[0.9 0.7 0.4]);%设置背景颜色
set(gcf,'Position',[0 0 1280 720]);%设置图像大小
柱状图(展示平均分数)
Y1=average_score;
X1=1:subject_num;
subplot(2,3,1);%设置为第一个的位置
bar(X1, Y1);
title('Subject-wise Average Marks');%改变标题
xlabel('Subject Name');%Xylabel('Marks');%Yaxis([ -inf inf 1 100])
yticks(0:10:100);
set(gca,'xTicklabel',subjects);%设置X轴坐标名称
set(gca,'XTickLabelRotation',45);%旋转45
饼图(展示各科分数等级人数比)
每一科实现成绩分级

以F级为例

    for j=1:stu_num
        if(sub_score(j,i)<50)
            sub_grade(j,i)=cellstr('F');
            F=F+1;
        end
    end
每一科画出3维饼图
画出基本图例

2019a以前版本画图自动忽略0项,若用以后版本则还需要一个简单循环来删除各等级中人数为0的项

    X= [F,P,CR,D,HD];
    labels={'F','P','CR','D','HD'};%X轴
    ax=subplot(2,3,i+1);%饼图位置
    explode=[1 1 1 1 1];
    pie3(X,explode,labels);
    title(subjects(i));
颜色调整

为了保证在有数据为0的情况下依旧能正常着色
需要这样一个循环处理来将0项对应的颜色从颜色列表中删除

    grade_color=[1,0,0;1,1,0;0,0,1;0,1,1;0,1,0];
    q=0;
    for p=1:5
        if (X(p)==0)
            grade_color(p-q,:)=[];
            q=q+1;
        end
    end
    colormap(ax,grade_color)

PART B

整合数据到结构体中

因为题目要求为1*n样式,故此处使用一下转置

stu_info(:,4:subject_num+3)=sub_grade;
s=(cell2struct(stu_info,str_info(1,:),2))';

输出示例

PART A

图片输出示例

PART B

DIO的成绩

完整代码

本文章仅供参考,群里问问题的实在是太多了,这就只是一个实现思路而已.
为防止剽窃,完整代码后续更新.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值