Help with plotting data in a mat file

4 views (last 30 days)
Hi, I have series of data that I need to plot, however, the sum is not working properly, the values are not correct. Attached is my code.
for i=1:1:30
F = "Fuel";
E = "Emissions";
P = "Power";
R = "RPM";
CO = "CO_2";
SO = "SOx";
NO = "NOx";
Q = int2str(i);
if i ==27 || i == 1 || i == 2 || i ==3
else
%% Fuel consumption
A(i) = load(append(F,Q)); % Load Fuel Data
B(i)= sum(A(i).Fuel(2,:)); % Sum Daily Fuel Data
G(i) = B(i)/24; % Daily Fuel / Hr
C = sum(B); % Total fuel Consumption in a month
D = C/26; % Average Fuel Consumption in a month
Z = sum(G)/26; % Daily Average Fuel / Hr
%% Emissions
A1(i) = load(append(E,Q)); % Load Emission Data
B1(i)= sum(A1(i).Emissions(2,:)); % Sum Daily Fuel Data
G1(i) = B1(i)/24; % Daily Emission / Hr
C1 = sum(B1); % Total Emission in a month
D1 = C1/26; % Average Emission in a month
Z1 = sum(G1)/26; % Daily Emission / Hr
%% Power
A2(i) = load(append(P,Q)); % Load Power Data
B2(i)= 2*0.25*sum(A2(i).Power(2,:)); % Sum Daily Fuel Data
G2(i) = B2(i)/24; % Daily Average / Hr
C2 = sum(B2); % Total Power in a month
D2 = C2/26; % Average Power in a month
Z2 = sum(G2)/26; %Daily Power / Hr
%% RPM
A3(i) = load(append(R,Q)); % Load Rpm Data
B3(i)= sum(A3(i).RPM(2,:)); % Sum Daily rpm Data
C3 = sum(B3); % Total rpm Data in a mont
D3 = C3/26; % Average rpm in a month
%% NOX EMISSION
A4(i) = load(append(NO,Q)); % Load Fuel Data
B4(i)= sum(A4(i).NOx(2,:)); % Sum Daily Fuel Data
G4(i) = B4(i)/24; % Daily Fuel / Hr
C4 = sum(B4); % Total fuel Consumption in a month
D4 = C4/26; % Average Fuel Consumption in a month
Z4 = sum(G4)/26; % Daily Average Fuel / Hr
%
%% SOX EMISSION
A5(i) = load(append(SO,Q)); % Load Fuel Data
B5(i)= sum(A5(i).SOx(2,:)); % Sum Daily Fuel Data
G5(i) = B5(i)/24; % Daily Fuel / Hr
C5 = sum(B5); % Total fuel Consumption in a month
D5 = C5/26; % Average Fuel Consumption in a month
Z5 = sum(G5)/26; % Daily Average Fuel / Hr
%
%% CO2 EMISSION
A6(i) = load(append(CO,Q)); % Load Fuel Data
B6(i)= sum(A6(i).CO2(2,:)) % Sum Daily Fuel Data
G6(i) = B6(i)/24; % Daily Fuel / Hr
C6 = sum(B6); % Total fuel Consumption in a month
D6 = C6/26; % Average Fuel Consumption in a month
Z6 = sum(G6)/26; % Daily Average Fuel / Hr
end
end
figure(1)
subplot(2,3,1)
yline(D,'r')
hold on
bar(B,'b')
xlabel ('Days of the Month');
ylabel ('Fuel Consumption (Kg)')
title('Daily Fuel consumption of marine diesel engine')
legend('Average Fuel','Daily Fuel')
grid on
subplot(2,3,2)
yline(D1,'r')
hold on
bar(B1,'b')
xlabel ('Days of the Month');
ylabel ('Emissions (Kg)')
title('Daily Emission of marine diesel engine')
legend('Average Emission','Daily Emission')
grid on
subplot(2,3,3)
yline(D2,'r')
hold on
bar(B2,'b')
xlabel ('Days of the Month');
ylabel ('Power (KWh)')
title('Daily Average load of marine diesel engine')
legend('Average load','Daily Power')
grid on
t = annotation('textbox', [0.44, 0.40, 0.1, 0.1], 'String', "SIMULATION SUMMARY");
t.FontSize = 16;
t.FontWeight ='bold';
t.LineWidth = 2;
t.Color = 'r';
t.EdgeColor = 'r';
annotation('textbox', [0.15, 0.30, 0.1, 0.1], 'String', "Total Fuel Consumption = " + C + " Kg ")
annotation('textbox', [0.15, 0.20, 0.1, 0.1], 'String', "Average Fuel Consumption = " + D + " Kg ")
annotation('textbox', [0.45, 0.30, 0.1, 0.1], 'String', "Total NOX Emission = " + C4 + " Kg ")
annotation('textbox', [0.45, 0.25, 0.1, 0.1], 'String', "Total SOX Emission = " + C5 + " Kg ")
annotation('textbox', [0.45, 0.20, 0.1, 0.1], 'String', "Total CO_2 Emission = " + C6 + " Kg ")
annotation('textbox', [0.45, 0.15, 0.1, 0.1], 'String', "Total Emission = " + C1 + " Kg ")
annotation('textbox', [0.45, 0.10, 0.1, 0.1], 'String', "Average Emission = " + D1 + " Kg ")
annotation('textbox', [0.73, 0.30, 0.1, 0.1], 'String', "Daily Average Power = " + sum(G2)+" Kwh")
  2 Comments
Cris LaPierre
Cris LaPierre on 17 Jun 2021
A quick check appears to show that it is calculating a sum. Which sums are incorrect? What should the correct values be?
chizom wornu
chizom wornu on 17 Jun 2021
Edited: chizom wornu on 17 Jun 2021
For, instance I manually calculated the sum for the first file, the value obtained is different when I run the script.
This line of code is where the sum does not work
B(i)= sum(A(i).Fuel(2,:)); % Sum Daily Fuel Data

Sign in to comment.

Accepted Answer

Scott MacKenzie
Scott MacKenzie on 17 Jun 2021
I think the error might be in how you are verifying your script. I ran your script and generated the plots. Here is what I see in the command window for the expression in the line of code you provided:
>> sum(A(i).Fuel(2,:))
ans =
1196.7
That is the correct value, but it is for the last day in the month. Did you mistakenly think it was for the 1st bar?
Consider this by comparison:
>> sum(A(4).Fuel(2,:))
ans =
833.71
This value is also correct, but it is for the 4th day of the month, which corresponds to the first non-zero bar in the chart:
Your script is fine, as best I can tell.
BTW, you should probably move the string assignments and the C, D, and Z calculations outside the for-loop. They only need to execute once.
  2 Comments
Cris LaPierre
Cris LaPierre on 17 Jun 2021
You need to be aware of what is happinging inside your loop (data for a single day) vs what is happening after the for loop finishes (data for the entire month).
for i=[4:26 28:30]
F = "Fuel";
Q = int2str(i);
A(i) = load(append(F,Q)); % Load Fuel Data
B(i)= sum(A(i).Fuel(2,:)); % Sum Daily Fuel Data
G(i) = B(i)/24; % Daily Fuel / Hr
end
B
B = 1×30
1.0e+03 * 0 0 0 0.8337 1.2232 1.8735 1.5742 1.5302 1.3855 1.5622 1.5687 1.4690 1.4644 1.1596 1.3225 1.8735 0.7340 1.2916 0.9036 1.4509 1.6663 1.5512 1.5002 1.6155 0.7468 0.4044 0 0.8731 1.2989 1.1967
C = sum(B) % Total fuel Consumption in a month
C = 3.4073e+04
D = C/26 % Average Fuel Consumption in a month
D = 1.3105e+03
Z = sum(G)/26 % Daily Average Fuel / Hr
Z = 54.6049
chizom wornu
chizom wornu on 17 Jun 2021
Thank you all, the code was actually right. I didnt change the folder so it was ploting different data from what i want it to plot.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!