You are on page 1of 18

PROJECT 2

FACULTY OF MECHANICAL ENGINEERING


SEMESTER 1 2022/2023
SEMM1013 – PROGRAMMING FOR ENGINEERS
TITLE : MATLAB PROJECT (INDIVIDUAL)

NAME : AKHMAL YOUNESS BIN HAERUL


MATRIC NUMBER : A22EM0039
SECTION : 01
LECTURER : DR KHAIRUL
TABLE OF CONTENTS

Table of Contents 2

Introduction 3

Flowchart 4

Result & Discussion 5-9

Conclusion 9

Source code 10-17

References 18

2
INTRODUCTION

MATLAB is a programming platform that is used to analyse and design system and
products that transform our world. The platform is specifically designed for engineers and
scientists because it provides an interactive environment for algorithm development, data
visualisation, data analysis, and numerical computation. The heart of MATLAB is the
MATLAB language, a matrix-based language allowing the most natural expression of
computational mathematics.

In this project, the main objective is to use MATLAB data structures and file
input/output functions to read and write the data from and to text files. The methodology used
in the project is by writing a main function consist of 4 sub-functions to create a database that
displays the result summary. Information from a data file will be extracted into the program.
After displaying the result summary, the information will be imported from the database to a
text file.

The project is about calculating the final marks and displaying the final grades of
students for the first semester of subject code SEMM1013. The data file is consist of 30 names,
matric numbers and marks for certain “simulated” course. The marks given are Manual 1-4,
CAD 1-4, T1 & T2 and PRJs. The overall average mark and grade will be displayed in the
output along with the marks and grades of the students and a grade distribution plot of the class
performance. Lastly, the output will be extracted into 2 text files in which the first one is
arranged according to names in alphabetical order and the other one is arranged according to
the matric number from largest to smallest.

3
FLOWCHART
Overall flowchart of MATLAB program :

START

Read
Command
Window

Main function

Read Sort database Sort ID number Write


database alphabetically from largest to database
from file by name smallest data into files

Extract result Display result Plot graph of


into text files summary grade
distribution

END

4
RESULT & DISCUSSION
The data file in which names, matric number, and marks for certain “simulated” course are
extracted from is shown in figure 1 :

Figure 1 : Marks.dat which consist names, matric number, and marks for certain “simulated”
course

The functions of program will run if correct input of data file is written in the command
window. From figure 2, the data file that is used is Marks.dat and text files are Result1.txt,
Result2.txt where Marks.dat contains the names, matric number, and the marks for courses
taken by the students and Result1.txt and Result2.txt will be the files in which data are
inserted after running the codes.

Figure 2 : Correct input arguments for running main function

5
The result when running the MATLAB program is shown in figure 3 :

Figure 3 : Results Summary

After running the MATLAB program, the output will be extracted into a text file. Figure 4
shows the data file sorted according to names in alphabetical order and Figure 5 shows the
data file sorted according to the id numbers from largest to smallest.

6
Figure 4 : Text file which the data is sorted according to names in alphabetical order

7
Figure 5 : Text file which the data file sorted according to the id numbers from largest to
smallest

A grade distribution plot of the class performance showing the number of students in each
grade category is formed when running the MATLAB program like as shown in Figure 6

Figure 6 : Grade distribution plot of the class performance

8
The program will not run if data file is not inserted into the command window. Figure 7 and
Figure 8 shows the error encountered when program is run without inserting data file :

Figure 7 : Error encountered when data is run without inserting data file

Figure 8 : Program cannot run when input arguments are not inserted

CONCLUSION
To conclude everything, programming platforms like MATLAB plays a big part in an
engineer’s life as interactive environment in terms of programming can be provided. Based on
the results gained through the program, the objective of this project is achieved. After using
MATLAB to complete this project, I am determined to study harder in using MATLAB to
execute more programs in the future.

9
SOURCE CODE
The codes in M-file used in the MATLAB program is as shown below :

%
% Name: Akhmal Youness bin Haerul
% Class: SEMM1013 Semester 1 Session 2022/2023
% Matlab Project Title: Coursework Result
% Name of File: Akhmal_Haerul_MProject.m
%
function [] = Akhmal_Haerul_MProject(mark,Result1,Result2)

% Read in a database from a file.


[Marks] = read_database_from_file(mark);
% Sort the database alphabetically by name.
[~,index] = sort_by_name(Marks);
% Write the name, ID number, assignment average, exam average to an output file.
write_database_to_file(Marks,index,Result1);
% Sort the database by ID number, largest to smallest.
[~,index1] = sort_by_id_number(Marks);
% Write the name, ID number, assignment average, exam average to an output file.
write_database_to_file(Marks,index1,Result2);

No = Marks.No;
Name = Marks.Name;
ID_number = Marks.Matrik;
Manual_1 = Marks.MANUAL1;
Manual_2 = Marks.MANUAL2;
Manual_3 = Marks.MANUAL3;
Manual_4 = Marks.MANUAL4;
CAD_1 = Marks.CAD1;

10
CAD_2 = Marks.CAD2;
CAD_3 = Marks.CAD3;
CAD_4 = Marks.CAD4;
Test1_Q1 = Marks.TEST1Q1;
Test1_Q2 = Marks.TEST1Q2;
Test2_Q1 = Marks.TEST2Q1;
Test2_Q2 = Marks.TEST2Q1;
Test2_Q3 = Marks.TEST2Q2;
Test2_Q4 = Marks.TEST2Q3;
Project_Assembly_Drawing = Marks.PROJECTASEMBLYDRAWING;
Project_Part_Drawing = Marks.PROJECTPARTDRAWING;
Project_Modern_Tools = Marks.PROJECTMODERNTOOLS;
Test40 =
(((Test1_Q1+Test1_Q2)./200).*20)+(((Test2_Q1+Test2_Q2+Test2_Q3+Test2_Q4)./100).*2
0);
Assignment30 =
(((Manual_1+Manual_2+Manual_3+Manual_4)./40).*20)+(((CAD_1+CAD_2+CAD_3+CA
D_4)./40).*10);
Project30 = (Project_Assembly_Drawing+Project_Part_Drawing+Project_Modern_Tools);
Final_Marks = Test40+Assignment30+Project30;
no = length(No);
Overall_Average_Marks = (sum(Final_Marks))/no;
if (Overall_Average_Marks>=90)
avg_grade = 'A+';
elseif (Overall_Average_Marks>=80)
avg_grade = 'A';
elseif (Overall_Average_Marks>=75)
avg_grade = 'A-';
elseif (Overall_Average_Marks>=70)
avg_grade = 'B+';
elseif (Overall_Average_Marks>=65)
avg_grade = 'B';

11
elseif (Overall_Average_Marks>=60)
avg_grade = 'B-';
elseif (Overall_Average_Marks>=55)
avg_grade = 'C+';
elseif (Overall_Average_Marks>=50)
avg_grade = 'C';
elseif (Overall_Average_Marks>=45)
avg_grade = 'C-';
elseif (Overall_Average_Marks>=40)
avg_grade = 'D+';
elseif (Overall_Average_Marks>=35)
avg_grade = 'D';
elseif (Overall_Average_Marks>=30)
avg_grade = 'D-';
else
avg_grade = 'E';
end

fprintf("\n\nRESULTS SUMMARY\n")
fprintf("\nSubject Code: SEMM1013\n");
fprintf("Semester: 1 \n");
fprintf("No of Students: %.0f\n",no);
fprintf("The Overall Average Mark = %.2f\t%s\n\n\n",Overall_Average_Marks,avg_grade);
fprintf("<#>\t<Name>\t<ID>\t<Final Mark>\t<Gred>\n");

c=zeros(1,13);
for i = 1:30
if (Final_Marks(i)>=90)
grade = 'A+';

12
c(1)=c(1)+1;
elseif (Final_Marks(i)>=80)
grade = 'A';
c(2)=c(2)+1;
elseif (Final_Marks(i)>=75)
grade = 'A-';
c(3)=c(3)+1;
elseif (Final_Marks(i)>=70)
grade = 'B+';
c(4)=c(4)+1;
elseif (Final_Marks(i)>=65)
grade = 'B';
c(5)=c(5)+1;
elseif (Final_Marks(i)>=60)
grade = 'B-';
c(6)=c(6)+1;
elseif (Final_Marks(i)>=55)
grade = 'C+';
c(7)=c(7)+1;
elseif (Final_Marks(i)>=50)
grade = 'C';
c(8)=c(8)+1;
elseif (Final_Marks(i)>=45)
grade = 'C-';
c(9)=c(9)+1;
elseif (Final_Marks(i)>=40)
grade = 'D+';
c(10)=c(10)+1;
elseif (Final_Marks(i)>=35)
grade = 'D';

13
c(11)=c(11)+1;
elseif (Final_Marks(i)>=30)
grade = 'D-';
c(12)=c(12)+1;
else
grade = 'E';
c(13)=c(13)+1;
end
fprintf("\n%d\t%s\t%d\t%.2f\t%s",No(i),Name(i),ID_number(i),Final_Marks(i),grade);
end

Grade={'A+','A','A-','B+','B','B-','C+','C','C-','D+','D','D-','E'};
barh(c);
xlabel('Number of Students');
ylabel('Grades');
yticklabels(Grade);

end % function Yourname_MProject

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%
% contains the following embedded functions %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%
function [Marks] = read_database_from_file(file)
fid = fopen(file,'r');
%% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 20);

% Specify range and delimiter


opts.DataLines = [3, Inf];

14
opts.Delimiter = "\t";

% Specify column names and types


opts.VariableNames = ["No", "Name", "Matrik", "MANUAL1", "MANUAL2",
"MANUAL3", "MANUAL4", "CAD1", "CAD2", "CAD3", "CAD4", "TEST1Q1",
"TEST1Q2", "TEST2Q1", "TEST2Q2", "TEST2Q3", "TEST2Q4",
"PROJECTASEMBLYDRAWING", "PROJECTPARTDRAWING",
"PROJECTMODERNTOOLS"];
opts.VariableTypes = ["double", "string", "double", "double", "double", "double", "double",
"double", "double", "double", "double", "double", "double", "double", "double", "double",
"double", "double", "double", "double"];

% Specify file level properties


opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";

% Specify variable properties


opts = setvaropts(opts, "Name", "WhitespaceRule", "preserve");
opts = setvaropts(opts, "Name", "EmptyFieldRule", "auto");
opts = setvaropts(opts, ["No", "Matrik", "MANUAL1", "MANUAL2", "MANUAL3",
"MANUAL4", "CAD1", "CAD2", "CAD3", "CAD4", "TEST1Q1", "TEST1Q2",
"TEST2Q1", "TEST2Q2", "TEST2Q3", "TEST2Q4", "PROJECTASEMBLYDRAWING",
"PROJECTPARTDRAWING", "PROJECTMODERNTOOLS"], "FillValue", 0);

% Import the data


Marks = readtable(file, opts);
closeresult = fclose(fid);
end % function read_database_from_file
%
%
function [] = write_database_to_file(Marks,index,Output_file)
fid = fopen(Output_file,'a');
Name = Marks.Name(index);
ID_number = Marks.Matrik(index);
15
Manual_1 = Marks.MANUAL1;
Manual_2 = Marks.MANUAL2;
Manual_3 = Marks.MANUAL3;
Manual_4 = Marks.MANUAL4;
CAD_1 = Marks.CAD1;
CAD_2 = Marks.CAD2;
CAD_3 = Marks.CAD3;
CAD_4 = Marks.CAD4;
Test1_Q1 = Marks.TEST1Q1;
Test1_Q2 = Marks.TEST1Q2;
Test2_Q1 = Marks.TEST2Q1;
Test2_Q2 = Marks.TEST2Q1;
Test2_Q3 = Marks.TEST2Q2;
Test2_Q4 = Marks.TEST2Q3;
Q_Average=((((Manual_1./10)+(Manual_2./10)+(Manual_3./10)+(Manual_4./10)+(CAD_1./
10)+(CAD_2./10)+(CAD_3./10)+(CAD_4./10))./8)*100);
E_Average =
((((Test1_Q1./100)+(Test1_Q2./100)+(Test2_Q1./30)+(Test2_Q2./16)+(Test2_Q3./19)+(Test
2_Q4./35))./6).*100);
Quiz_Average = Q_Average(index);
Exam_Average = E_Average(index);
fprintf(fid,"Name\tMatrik No\tQuiz Average\tExam Average\n");
for i=1:30

fprintf(fid,'%s\t%.0f\t%.2f\t%.2f\n',Name(i),ID_number(i),Quiz_Average(i),Exam_Average(i
));
end
fclose(fid);
end % function write_database_to_file
%
%
function [out,index] = sort_by_name(Marks)

16
Name = table2array(Marks(:,2));
[out,index] = sort(Name);
end % function sort_by_name
%
%
function [out,index1] = sort_by_id_number(Marks)
idnumber = table2array(Marks(:,3));
[out,index1] = sort(idnumber,'descend');
end % function sort_by_id_number
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%

17
REFERENCES
1. What is MATLAB? MATLAB &amp; Simulink. (n.d.). Retrieved February 2, 2023, from
https://www.mathworks.com/discovery/what-is-matlab.html

18

You might also like