Beruflich Dokumente
Kultur Dokumente
Objective:-
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);
[annualraindata,averagerainlevel]=subtractaverage(sannualdata);
display(averagerainlevel);
display(annualraindata);
function[data,average]=subtractaverage(x)
average = mean(x(:,2));
data = x; data(:,2)=x(:,2)-
average;
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);