Digital Image Processing (DIP)
Lab 1
Title: Writing and Running Programs with Matlab.
Problem 1.1.
Using the Matlab editor/notepad editor generate the following two data files.
The first data file you should save under the name winter_rain.dat. It should contain the
following values:
5 2.5
6 3.3
7 2.4
8 3.2
9 3.6
10 2.7
The first data file you should save under the name summer_rain.dat. It should contain the
following values:
11 2.5
12 2.3
1 2.4
2 1.8
3 1.6
4 2.4
Both data files should be stored in the same directory for example ~/Labs/
The first column of each file denotes the month (for example 5 corresponds to May) and the second column gives the average monthly rain water level in milimeters.
Problem 1.2.
Using the Matlab editor write a Matlab program performing the following operations:
1. Reads the from file winter_rain.dat into array winterdata
2. Reads the from file summer_rain.dat into array summerdata
%Lab01.m
%Read the input data
f1=fopen('winter.dat','r');
f2=fopen('summer.dat','r');
winterdata=fscanf(f1,'%d %f',[2,6]);
summerdata=fscanf(f2,'%d %f',[2,6]);
fclose(f1);
fclose(f2);
winterdata=winterdata';
summerdata=summerdata';
3. Generates a single plot representing rain levels across the whole year (starting from
January and ending at December). This plot should be base on data stored in both
arrays: summerdata and winterdata.
for i=1:6
for j=1:2
annualdata(i,j)=winterdata(i,j);
end
end
for i=1:6
for j=1:2
annualdata(i+6,j)=summerdata(i,j);
end
end
sannualdata = sortrows(annualdata);
%plot the separate summer and winter graphs
figure(1);
plot(sannualdata(:,1),sannualdata(:,2));
xlabel('month');
ylabel('rain level in mm');
axis([1 12 0 4]);
grid;
4. Substracts the annual average rain level value from the rain levels of each month and
stores the results in the array annualraindata. This should be done by a user-defined
function subtractaverage.m stored in the same directory (for example ~/Labs/ ).
[annualraindata,averagerainlevel]=subtractaverage(sannualdata);
display(averagerainlevel);
display(annualraindata);
function[data,average]=subtractaverage(x)
average = mean(x(:,2));
data = x;
data(:,2)=x(:,2)-average;
5. Prints out the average annual rain level in mm.
6. Generates a continuous-line plots, representing annual rain levels based on the data in
annualraindata. Note that in this plot the zero level represents an annual average rain
level.
7. Stores the data from array annualraindata in the text file annualrain.dat. This
should be saved in the same directory as the original input files (for example
~/Labs/).
%plot the separate summer and winter graphs
figure(2);
plot(annualraindata(:,1),annualraindata(:,2));
xlabel('month');
ylabel('rain level in mm, zero represents annual average');
axis([1 12 -4 4]);
grid;
f3=fopen('anualrain.dat','w');
%fwrite(f3,annualraindata);
for i=1:12
fprintf(f3,'%d %f\n',annualraindata(i,1),annualraindata(i,2));
end
fclose(f3);