5. IMPORTING DATA AND USING FUNCTION .M FILES
This chapter introduces the essentials for your Tutorial Group Matlab Assignment (40% of module mark)
Everyone should complete this chapter individually before commencing work on the group assignment.
5.1.1 IMPORTING DATA USING .M FILES
So far, the command window was used for input and output using the formats:
Getting input: Expression = input(‘Text requesting input’)
Displaying Output: disp(Expression) or fprintf(Expression)
Large datasets can be imported into Matlab using import:
Importing data: Expression = importdata(‘Filename.csv’)
5.2 MATLAB ASSIGNMENT PREPARATION
5.2.1 IMPORTING DATA FOR THE MATLAB ASSIGNMENT
In your Matlab Assignment, you will analyse run profile data for the Bloodhound Supersonic Car. The aim is to reach 1000 mph safely.
The constraints on the world record attempt are:
1. The car must exceed 1000 mph for a distance of at least 1 mile
2. The G-force experienced by the driver Andy Green must not exceed 3.5g
3. The total distance travelled must not exceed 10 miles
The car is accelerated using a Rocket engine boosted by a rocket. Parachutes are deployed for deceleration at 50 s. The ambient temperature affects the performance of the car. Varying the rocket start time and simluating the temperature alters the way the car performs.
You will be provided with nine run profiles:
Run Profile Rocket Start Time (s) Temperature (°C)
1 8 7
2 12 7
3 16 7
4 8 15
5 12 15
6 16 15
7 8 24
8 12 24
9 16 24
You are given the distance travelled in metres and Rocket fuel burned in kg for each run profile as .csv files.
Exercise 5.1
Download the following files from Blackboard:
• Distance.csv
• RocketFuelBurned.csv
Save them in your Current Folder.
Double-click on the Distance.csvfile in the Current Folder. This is a 931×9 matrix.
Each column has time-series distance data in metres for a different run profile of the Bloodhound SSC. Column 1 is Run Profile 1, column 2 is run profile 2, and so on. Each row is a different time, the timestep is 0.1s. Row 1 is at Time=0, row 2 is at Time=0.1s, row 3 is at Time=0.2s, and so on.
Double-click on the RocketFuelBurned.csv file. This is also a 931×9 matrix, it is structured in the same way as A1Distance.csv. Each column has time-series data on the amount of Rocket fuel burned in kg.
Exercise 5.2
Create a new .m file and save it as BHOpenFile.m
To import the bloodhound datafile into Matlab, type the following in the BHOpenFile.m file:
%Importing Data
Dist_met = importdata(‘Distance.csv’);
Look in the Workspace – you should see a new matrix called Dist_metis stored. Double-click to open it. Compare with the original BH_Distance.csv file accessed in the Content Folder. They should be identical.
Now use the same process to import the RocketFuelBurned.csv file into a matrix called RocketFuel_kg.
RocketFuel_kg=?????
Save the BHOpenFile.m file.
Run the BHOpenFile.m file.
Check the matrix RocketFuel_kg is the same as RocketFuelBurned.csv.
5.2.2 COMBINING AND USING FUNCTION M.FILES
The Bloodhound program you are creating will have four .m files. There will be one ‘Master’ file and three function files. The relationship between the files is shown below:
Information is passed between the files in the order shown on the arrows.
The Master file and Function files exchange information using the following syntax:
[outputs]= myfunction(inputs)
Exercise 5.3
Create a new .m file called ‘BHProgram.m’. This will be your Master File. The Master File will often be simple. Here, its only purpose is to link the function files.
In BHProgram.m, type the following:
% Bloodhound Program
clc
clear
close all
Name=[‘Your Name’];
StudentNo=[Your Student No.];
fprintf(‘This is the Bloodhound Program of %s, Student No. %dn’,Name, StudentNo)
%Import data
[Time_s, Dist_met,RocketFuel_kg]=BHOpenFile(Name,StudentNo);
Save the BHProgram.m file.
Exercise 5.4
Open the BHOpenFile.m, type or copy & paste the following at the very top of the file:
function [Time_s, Dist_met,RocketFuel_kg]=BHOpenFile(Name,StudentNo )
At the bottom of the file, complete the following script to plot the imported data:
%Plot the imported data
%Define time vector to 93 seconds with timestep of 0.1s
Time_s=[0:???:93]
%Draw figure
figure
%for loop across columns of data
for ic=1:?
plot(Time_s,Dist_met(:,ic))
hold ??
end
grid ??
xlabel(‘Time (s)’)
ylabel(‘Distance (m)’)
title(‘Data Read from File’)
Save BHOpenFile.m.
Run the BHProgram.m file to check that it plots the following graph:
Referring to the syntax: [outputs]= myfunction(inputs), the program schematic is shown on the right:
The Master file (BHProgram.m) is run first. The 8th line executes Function #1 (BHOpenFile.m) to open the files.
Input data is passed from the Master file to Function #1 (Name and Student_No)
Function #1 imports the data (Dist_met, RocketFuel_kg) and defines Time_s.
Output data (Time_s, Dist_met, RocketFuel_kg) is passed back to the Master file.
So far, the program consists of the Master file BHProgram.m and Function#1 BHOpenFile.m
Now we need to add Function#2 BHProcess.m. In this .m file we will perform analysis and calculations on the data imported.
Exercise 5.5
The graph that was plotted in Exercise 5.4 shows distance in metres. Refer to page 1 of this chapter. How does the data need to be processed in order to determine whether the criteria are met?
Create a new .m file named BHProcess.m. In the first line, type the appropriate function syntax, using the following generic format:
[outputs]= myfunction(inputs)
• Inputs are Time_s, Dist_met and RocketFuel_kg
• Outputs are Dist_miles,Velocity_milesph,Velocity_metsps, Acc_metsps, G_force. See BHOpenFile.m for guidance.
Complete the following to convert distance in metres to distance in miles:
%Distance conversion from metric to imperial
Dist_miles=Dist_met*?????;
Complete the following to calculate velocity in miles per hour:
%Velocity calculation in metric and imperial
Velocity_milesph(1,1:9)=0;
for ic=1:9
for iv=2:931
Velocity_milesph(iv,ic)=(Dist_miles(iv,ic)-Dist_miles(iv-1,ic))/(0.1*???????);
end
end
In BHProcess.m, use the above as a guide to create the following data vectors:
Velocity_metsps Velocity in metres per second at each timestep
Acc_metsps Acceleration in ms-2 at each timestep
G_force G-force in g at each timestep
Fuel Fuel consumption at each timestep
CO2 CO2 burned at each timestep
How can you make the code efficient? For example, is there a more efficient way of specifying the Rocket fuel burned or CO2 emitted?
5.2.3 PLOTTING
Exercise 5.6
Create a new .m file called ‘BHPlot.m’. Copy the following:
function BHPlot(Dist_miles, Velocity_milesph, Acc_metsps, G_force, Time_s)
%Formatting for Run Profile plots
PlotFormat=[{‘m-‘},’m:’,’m-.’,’b-‘,’b:’,’b-.’,’g-‘,’g:’,’g-.’]
figure
for ip=1:9
plot(Dist_miles(:,ip),Velocity_milesph(:,ip),PlotFormat{ip})
hold on
end
legend(‘1:R8s,T7′,’2:R12s,T7′,’3:R16s,T7′,’4:R8s,T15’,…
‘5:R12s,T15′,’6:R16s,T15′,’7:R8s,T24′,’8:R12s,T24′,’9:R16s,24’)
Save the BHPlot.m file.
At the end of BHProgram.m type:
BHPlot(Dist_miles, Velocity_milesph, Acc_metsps, G_force, Time_s)
Run BH_Program.m. The program should now create this formatted plot of the data with a legend:
The Legend holds a key to the Run Profile data, e.g. R8s is Rocket Start Time = 8s, T7 is Temperature 7 °C.
For a custom paper on the above topic, place your order now!
What We Offer:
• On-time delivery guarantee
• PhD-level writers
• Automatic plagiarism check
• 100% money-back guarantee
• 100% Privacy and Confidentiality
• High Quality custom-written paper
You May Also Like This:
- Real Estate Data
- most common file types imported into Excel
- program by C#
- analyzing data with multiple predictor variables and one continuous scale DV to investigate comparison of means.
- independent variables
- DMM 639 Wk 1 Principles of Disaster Exercises and Drills
- the effects of air pollution on vegetation.
- The CGI bin script
- Food Journal
- Case that defined you as a nurse, importance of debriefings, Stress nurses
- The Financials
- Infographic on preventative screenings or vaccines. Preventative screening (like colonoscopy) or vaccine (like pneumovax).
- SAS dataset. Q2
- Health Organisation Workforce Plan
- Econometric research using the IPUMS CPS data