how to calculate Minimunm, maximum and average values using the script
33 views (last 30 days)
Show older comments
Quasirat Tarfi
on 8 Mar 2018
Commented: Saad Hussain
on 7 May 2021
write a script to:
Extract the following data from the ev data (attached).
Pack T1 C
Pack T2 C
Pack T3 C
Pack T4 C
2. The extracted data represent temperature measured from four sensors located around the battery pack. Calculate minimum, maximum and average temperature for each sensor.
3. Use bar command to plot at the same figure the calculated minimum, maximum and average values for each sensor. Add legend, grid and title to the figure. Add labels to all axes.
1 Comment
Saad Hussain
on 7 May 2021
hey i was wondering if you were able to write up the exact code for it because the graph wont plot with this code, i have the exact same task
Accepted Answer
Pawel Jastrzebski
on 8 Mar 2018
Edited: Pawel Jastrzebski
on 8 Mar 2018
% Step 1: IMPORT DATA
fileName = 'Log_U0006387_160311_740d0.csv';
data = readtable(fileName)
% STEP 2: extract the data of interest to a new table
T_subData = data(:,{'PackT1C', 'PackT2C', 'PackT3C', 'PackT4C'})
% STEP 3: carry out required calculations
nomVal = mean(T_subData{:,:})
minVal = min(T_subData{:,:})
maxVal = max(T_subData{:,:})
% arrange data in respect the the TC
% preallocate
allDataRearraged = zeros(length(nomVal),3);
for i = 1:length(nomVal)
allDataRearraged(i,1) = minVal(i);
allDataRearraged(i,2) = nomVal(i);
allDataRearraged(i,3) = maxVal(i);
end
% STEP 4: plot the results
figure
bar(allDataRearraged)
title('Results')
xlabel('x values')
xticklabels(T_subData.Properties.VariableNames)
ylabel('Temperature')
legend({'min','nominal','max'},'Location','best')
More Answers (1)
Mohamed Mohamed Ali
on 7 Nov 2020
a) Import Data
This data is from https://www.data.gov/
Please go to the website first to look at all the possible Comma Separated Value (CSV) data that you can import. For simplicity, don’t search for the file on this site. Just get a feel for the data you can download. You can use this sight for your projects if desired.
Go to Canvas for our class (ECE105), and download the data file under week 10. The file is about 15Mb, and this file should end up in your download folder:
“Heart_Disease_Mortality_Data_Among_US_Adults__35___by_State_Territory_and_County (1).csv”
Open Matlab, and click on import data. It will show your files. Find your download folder, find the heart disease file, make sure it’s highlighted by clicking on it, and click on open in the lower right.
Matlab should open the file.
Under “Range” in the menu bar of your import window, change the range to H2:H59077. Then click just outside of the range box, and Matlab should highlight just that one column. Column H should be highlighted, and you’re about to import a 50976 row x 1 column matrix of numbers.
In the “Output Type” box right next to the “Range “ box, use the drop down menu and select “numeric matrix”. That gives you a matrix rather than the default table of numbers. In the box next to output type, for cells with data that are unimportable replace “NaN” (which is the default) to 0.0 (number 0). If you don’t do this your mean value will come out to NaN.
Now you should be set, and click on the green check mark in the upper right. It will take about 10 to 20 seconds to import the data depending on your computer.
Close the import window, and look at your workspace. You should see the matrix.
I suggest changing the name because it’s too much to type.
In the command window type:
A= HeartDiseaseMortalityDataAmongUSAdults35byStateTerritoryandCoun;
Don’t forget the semicolon at the end or Matlab will put all those values in the command window. If it does, just use a clc command.
Now save your workspace. Click on “Save Workspace” in the menu bar, enter “Heart_Disease” as the file name, and click save. Check your MATLAB folder to make sure “Heart_Disease.mat” is in there.
b) Calculate maximum, mean and median
Now that you have the data, generate a script that calculates
Maximum, minimum, mean, and median of A. Minimum should be 0.
c) Use “fprintf “ command to come up with this:
“For 100,000 heart patients in the U.S., the max death rate by county is 6350.700000
the mean death rate is 199.293282
and the median death rate is 187.800000”
example:
fprintf('For 100,000 heart patients in the U.S., the max death rate by county is %f \n', a)
where “a” has been assigned to the maximum value
remember %f is the number you want and /n is a line feed
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!