%This files reads the data from energy.txt and plots out the change in KE & PE over time.
inData = fopen('energy.txt', 'r');
inData2 = fopen('heatcurrent.txt', 'r');
data = zeros(6,1);
data2 = zeros(6,1);
temp = 20;

% First get the number of data sets.
NoSets = fscanf(inData, '%d', 1);
NoSets2 = fscanf(inData2, '%d', 1);
TEnergy = zeros(1,NoSets);   % Total energy. KE + PE.
KE = zeros(1,NoSets);        % Kinetic energy data.
PE = zeros(1,NoSets);        % Potential energy data.
TE = zeros(1,NoSets);        % Measured temperature data.
TS = zeros(1,NoSets);        % Set temperature data.
t = zeros(1,NoSets);         % Time for all above.
HE = zeros(1,NoSets2);       % Heat current data.
t2 = zeros(1,NoSets2);       % Time for heat current.

% Now loop to get all the KE & PE data out.
for i = 1:NoSets
  data = fscanf(inData, '%d %e %e %e %e %e', 6);
  t(1,i) = data(1,1);
  PE(1,i) = data(2,1);
  KE(1,i) = data(3,1);
  TEnergy(1,i) = data(4,1);
  TE(1,i) = data(5,1);
  TS(1,i) = data(6,1);
end;

% Now loop to get all the heat current data out.
for i = 1:NoSets2
  data2 = fscanf(inData2, '%d %e %e %e %e', 5);
  HE(1,i) = data2(5,1);
  t(1,i) = data2(1,1);
end;

% Plotting TE vs time together with the Tset value.
subplot(5,1,1); hold on
plot(t,TE);
plot(t,TS,'Color', 'red');
xlabel('Time (tstep)');
ylabel('TE (K)');
title('TE vs. tstep (The red line represents the set temperature)');

% Plotting KE vs time.
subplot(5,1,2); hold on
plot(t,KE);
xlabel('Time (tstep)');
ylabel('KE (J)');
title('KE vs. tstep');

% Plotting PE vs time.
subplot(5,1,3); hold on
plot(t,PE);
xlabel('Time (tstep)');
ylabel('PE (J)');
title('PE vs. tstep');

% Plotting Total energy vs time.
subplot(5,1,4); hold on
plot(t,TEnergy);
xlabel('Time (tstep)');
ylabel('Total Energy (J)');
title('Total Energy vs. tstep');

% Plotting HE vs time.
subplot(5,1,5); hold on
plot(t,HE);
xlabel('Time (tstep)');
ylabel('HE (W)');
title('HE vs. tstep');

fclose(inData);