clc clear all close all % ABSTRACT disp('ABSTRACT') disp(' Cushed Can Energy and time') disp(' ') disp('INPUTS') filen='C:\Documents and Settings\djmille3\Desktop\total_energy_wtime_sodacan.txt'; fh=fopen(filen,'r'); fprintf(' The file location is:\n %s\n\n',filen) % CODE/OUTPUTS % reading the input file a=' '; % To get things started i=0; % counter for while-end loop while ischar(a)==true a=fgets(fh); % if there's a character, read the line if ischar(a)==true b=sscanf(a,'%f %f'); % what is on the line? i=i+1; time(i)=b(1); % make storage time vector energy(i)=b(2); % make storage energy vector end end % PLOTTING ENERGY VS. TIME hold on plot(time*10^6,energy,'g-','LineWidth',2) title({'Cushed Soda Can';'Total Energy (J) vs. Time (\mus)'}) xlabel('Time (\mus)') ylabel('Energy (J)') axis([0 600 0 180]) grid on hold off % FINDING MAX ENERGY WITH TIME menergy=energy(1); n=length(energy); % find number of elements for i=2:1:n if energy(i)>menergy menergy=energy(i); % current maximun energy emtime =time(i); % current time that max energy occurs end end disp('OUTPUTS') fprintf(' The maximun energy is %g J at %g sec\n',menergy,emtime)