File: ef230-2023-08/www/examples/quakeplot.m Download
%! Example of importing data and plotting
% University of Tennessee : 11/4/03 : Will Schleter
% Original project: http://ef.engr.utk.edu/ef101-2003/as/lab/as4-project

function quakeplot

clear all; close all; clc;

% get data
url='http://ef.engr.utk.edu/ef101-2003/as/lab/as4-project/northridge.txt';
fn='northridge.txt';
urlwrite(url,fn);
data = dlmread(fn,'',12,0); % read numeric data in
names = getnames(fn)

% get user selection of names
[citynums,ok] = listdlg('ListString',names);
if ok==0, error('User cancelled input'); end
% make the plots
figure(1);
tp = length(citynums)
for i=tp:-1:1
    ic=citynums(i);
    eqplot(i,names{ic},ic,data,tp);
end

title('Northridge Earthquake - 17 Jan 1994 - Joe Blow');
set(gcf,'units','normalized','position',[.2 .1 .6 .8]); % make figure window taller
return

function eqplot(n,citname,c,data,tp)
    t = 0:.02:30;
    a = data(:,c)./100./9.81; %extract and convert to g's

    %calc stats
    a_avg = avg(a);
    a_std = SD(a);
    a_rms = RMS(a);

    %plot data
    if tp>3
        nr=ceil(tp/2);
        nc=2;
    else
        nr=tp
        nc=1
    end
    subplot(nr,nc,n);
    plot(t,a,'k-');

    % the city label...
    text(15,1.25,['Station: ' citname ],'HorizontalAlign','center');

    % the stats label...
    txt = sprintf('Mean=%.3g  SD=%.3g  RMS=%.3g',a_avg,a_std,a_rms);
    text(15,-1.25, txt, ...
         'fontsize',8,'HorizontalAlign','center');

    % plot annotation
    axis([0 30 -1.5 1.5]);
    xlabel('Time (sec)');
    ylabel('Acceleration (g)');

return

function r=avg(d)
r=sum(d)./length(d);
return

function r=SD(d);
m=avg(d);
n=length(d);
r=sqrt(sum(((d-m).^2))./(n-1));
return

function r = RMS(d)
r=sqrt(avg(d.^2));
return

function names=getnames(fn)
% read the names from the file - this isn't pretty:)
fh = fopen(fn,'r');
names=cell(1,10);
for i=1:10
    line = fgets(fh)
    if i>=9
        for j=0:9
            k=3+j*12;
            if k+11>length(line), break, end;
            names{j+1} = [names{j+1} ' ' strtrim(line(k:k+11))];
        end
    end
end
fclose(fh)
names = strtrim(names);
return