File: ef230-2023-08/www/examples/plot_date_scale.m Download
%! Example of creating a plot with time of day on the x axis
% Solution to an old EF 102 homework circa 2003
% University of Tennessee : Will Schleter

%HW 4.3 part 1
clear all;
clc;
a = 5; b = 8.7; c = 3.3;
d = [17.2 -5.0 0.0 1.0];
e = [ 8.0; 5.5];
f = [1 pi 2];
g = [8 0 5; 4 8 2];
m = [1.3 3.0 5.2; 2.7 4.5 6.8];
n = [2 4; 9 8];

echo on
sin(f)
cos(c)
log(d) % gives warning and imaginary numbers
log10(d) % gives warning and imaginary numbers
exp(g)
sign(d)
tanh(d)
round(m)
ceil(m)
floor(m)
echo off

%HW 4.3 Part 2
% init
clear all;
close all;
myname = 'Joe Blow'; % define name here so it is easier to copy this HW:)

% given data, times in minutes, temps in deg F
r13time = [ 0 15 35 60 95 125 155 ];
r13temp = [ 66.4 66.9 67.5 68.5 69.8 70.5 71.2 ];
r108time = [ 0 25 40 70 110 150 ];
r108temp = [ 68.2 69.5 70.5 71.3 72.0 73.4 ];
r205time = [ 0 20 40 60 80 100 120 140 160 ];
r205temp = [ 72.1 72.5 72.9 73.4 74.0 74.2 74.3 74.4 74.5 ];

starttime = datenum(2002,10,29,8,10,0);

% conversion functions to avoid variable bloat and formula repetition
f2c = inline('5.*(t-32)./9','t');
c2k = inline('t + 273.16','t');
f2r = inline('t + 459.67','t');
% can't nest inline functions
% f2k = inline('c2k(f2c(t))','t');
f2k = inline('273.16 + 5.*(t-32)./9','t');
G = 9.81; % m/s/s, gravity accel, because we need a tie-in to science :(

% convert delta times to absolute
% combine with datetick command to get good x labels
r13time = datenum(0,0,0,0,r13time,0) + starttime;
r108time = datenum(0,0,0,0,r108time,0) + starttime;
r205time = datenum(0,0,0,0,r205time,0) + starttime;

% plot style variable so it can be the same in all plots
ps = {'b*-','ro--','g^:'};

% all data on one plot, I'd rather use hold on and multiple plot commands
figure('name','my fig 1','NumberTitle','off','MenuBar','none')
plot(r13time,r13temp,char(ps(1)), ...
     r108time,r108temp,char(ps(2)), ...
     r205time,r205temp,char(ps(3)) );
% figure 1 annotation
title(['Plot title - by ' myname ' on ' date]);
legend('Room 13^1','Room 108^2','Room 205^3',4); %superscripts reference text note
xlabel(['Time of day (' datestr(starttime,1) ')']);
datetick('x',15) % displays x labels as times of form hh:mm
ylabel('Room Temp (^oF)');
% add text note per reqs, probably better in legend...
% cell type variable {} needed for variable length lines
text(datenum(2002,10,29,9,30,0),68, {'Notes:','1. ta1','2. ta2','3. ta3'});
% put figure to a file for importing into word
print -dmeta 'c:\test.wmf' -noui;
% put figure to a file for use on web page
print -dpng 'c:\test.png' -noui;


% three subplots with different units
% title only on top, xlabel only on bottom
figure(2);
subplot(3,1,1);
plot(r13time,f2c(r13temp),char(ps(1)), ...
    r108time,f2c(r108temp),char(ps(2)), ...
    r205time,f2c(r205temp),char(ps(3)));
datetick('x',15)
ylabel('Temp ^oC');
title({'Subplot title', ['by ' myname ' on ' date]});

subplot(3,1,2);
plot(r13time,f2k(r13temp),char(ps(1)), ...
    r108time,f2k(r108temp),char(ps(2)), ...
    r205time,f2k(r205temp),char(ps(3)));
datetick('x',15)
ylabel('Temp ^oK');

subplot(3,1,3);
plot(r13time,f2r(r13temp),char(ps(1)), ...
    r108time,f2r(r108temp),char(ps(2)), ...
    r205time,f2r(r205temp),char(ps(3)));
datetick('x',15)
xlabel(['Time of day (' datestr(starttime,1) ')']);
ylabel('Temp ^oR');
legend('Room 13','Room 108','Room 205');