Вы находитесь на странице: 1из 11

MATLAB: DATA INPUT OUTPUT 1

ASCII DATA

Loading Data

Suppose that we want to load a data stored in a text file like one shown below. The name of the file is
‘simpledata.txt’.

We can use MATLAB function load to load the data onto MATLAB workspace and save it as variable A
.
A = load('simpledata.txt');

Now, if we type A in MATLAB, we’ll get:

>> A

A =

1
2
3
4
5
4
3
2
1

Let us consider a more realistic file like file ‘students.txt’, which stores information students grade.

students.txt

017142031 Ina 73 2.76


027910908 Khalid 12 1.61
104148606 Mawi 85 3.04
118229053 Siti 98 3.80
129830779 Man_Kidal 92 3.51
370221320 Awie 51 2.23
391235525 Nurhaliza 87 3.69

integer string integer float

© Anuar Mikdad Muad – JKEES, Universiti Kebangsaan Malaysia


MATLAB: DATA INPUT OUTPUT 2

If we look at this file, we can see that the file can be separated into different types of data. Matrix number is
in integer form, students name is in character or string form, mark is in integer and CGPA is in float format.

Now, we are going to load the file in MATLAB and split it according to specific groups: Matrix Number,
Student’s Name, Score, and CGPA.

We are using textread function in MATLAB to load information contained in text file onto MATLAB
Workspace.

Type this at the MATLAB Workspace:

[MatrixNo, Name, Marks, CGPA] = textread('Students.txt','%d %s %d %f')

Then , it will give us this answer.

MatrixNo =

17142031
27910908
104148606
118229053
129830779
370221320
391235525

Name =

'Ina'
'Khalid'
'Mawi'
'Siti'
'Man_Kidal'
'Awie'
'Nurhaliza'

Marks =

73
12
85
98
92
51
87

CGPA =

2.7600
1.6100
3.0400
3.8000
3.5100
2.2300
3.6900

© Anuar Mikdad Muad – JKEES, Universiti Kebangsaan Malaysia


MATLAB: DATA INPUT OUTPUT 3

Saving Data

At the MATLAB Workspace, type

A = magic(3)

And we’ll get

A =

8 1 6
3 5 7
4 9 2

This data is in MATLAB Workspace. Now, we want to save this data into a text file (ascii format).

save mydata.txt A -ascii

Where save = saving function in MATLAB


mydata.txt = name of our file
A = variable
-ascii = save it in ASCII format

If we look at the folder that the file is being saved, we can see a newly created file. Open the file.

MAT FILE

If we type this kind of function in MATLAB Workspace


save mydata2 A

By default, MATLAB save variable, A in ‘firstdata.mat’ file. This is not an ascii file, but a binary file. If
we open it using Notepad program, it only display some weird characters. If we want to open it, then we
have to do in MATLAB environment.

load mydata2

© Anuar Mikdad Muad – JKEES, Universiti Kebangsaan Malaysia


MATLAB: DATA INPUT OUTPUT 4

LOW LEVEL I/O FUNCTIONS

If we want to make use the most of the data input output, then we must consider to utilize low level I/O
function available in MATLAB. The functions are just like functions in C/C++.

Name of Function Functionality


fopen Open the file

fread Read binary data


fwrite Write binary data
fgets/fgetl Read text strings from a file, line by line
fscanf Read formatted ASCII data
fprintf Write binary data

fclose Close the file

For example, in ‘iridium.txt’ file, there is information of various types of satellite. In this file, it has been
arranged in such a way that the name of the satellite comes first, or is located in the first line. The second
and third line is numerical data about this satellite (maybe it is information about latitude and longitude
coordinate of satellite path).

The fourth line in the file is a name of another satellite, followed by its data in the fifth and sixth line. This
style of arrangement is continued for another satellites.

IRIDIUM 08
1 24792U 97020A 98333.85311056 .00000370 00000-0 12493-3 0 2239
2 24792 86.3956 269.3093 0004288 73.9051 286.2597 14.34221913 82225
IRIDIUM 07
1 24793U 97020B 98333.84676827 .00000304 00000-0 10138-3 0 2151
2 24793 86.3969 269.3079 0004300 109.3983 250.7652 14.34221191 82223
IRIDIUM 06
1 24794U 97020C 98334.18926776 .00000012 00000-0 -27356-5 0 2127
2 24794 86.3974 269.1943 0003663 89.7462 270.4193 14.34218004 82275
IRIDIUM 05
1 24795U 97020D 98333.90384909 -.00000035 00000-0 -19563-4 0 2882
2 24795 86.3987 269.2496 0003771 53.6784 306.4761 14.34217495 82309
IRIDIUM 04
1 24796U 97020E 98333.85944779 .00000200 00000-0 64205-4 0 2463
2 24796 86.3964 269.3176 0002188 127.0683 233.0702 14.34218446 82246

IRIDIUM 08
1 24792U 97020A 98333.85311056 .00000370 00000-0 12493-3 0 2239
2 24792 86.3956 269.3093 0004288 73.9051 286.2597 14.34221913 82225

IRIDIUM 06
1 24794U 97020C 98334.18926776 .00000012 00000-0 -27356-5 0 2127
2 24794 86.3974 269.1943 0003663 89.7462 270.4193 14.34218004 82275

© Anuar Mikdad Muad – JKEES, Universiti Kebangsaan Malaysia


MATLAB: DATA INPUT OUTPUT 5

Now, we are going to split the data into each satellite.

% ====== LOW-LEVEL IO FUNCTIONS ===


% Open the ‘iridium.txt’ file
fid = fopen('iridium.txt');

x = 1;
while 1

% Get informations from text file line by line


satellite(x).type = fgetl(fid);
satellite(x).data1 = fgetl(fid);
satellite(x).data2 = fgetl(fid);

% If end of line, break


if ~isstr(satellite(x).type)
break
end
x = x + 1;
end

Now, at MATLAB Workspace, type

satellite

And MATLAB will display this…

satellite =

1x87 struct array with fields:


type
data1
data2

This means that we have already created a structure variable named satellite. And also, from the file,
we actually have 87 satellites.

If we want to get access to satellite number four, we just type

satellite(4)

And we get this…

ans =

type: 'IRIDIUM 05 '


data1: '1 24795U 97020D 98333.90384909 -.00000035 00000-0 -19563-4 0 2882'
data2: '2 24795 86.3987 269.2496 0003771 53.6784 306.4761 14.34217495 82309'

© Anuar Mikdad Muad – JKEES, Universiti Kebangsaan Malaysia


MATLAB: DATA INPUT OUTPUT 6

AUDIO DATA

Read audio signal from ‘waterSound.wav’ file

[Y,Fs] = wavread('harimau.wav');

Where Y = data is stored in variable, Y


Fs = sampling frequency, (in Hertz)

Now, plot the signal.

figure, plot(Y)

How about listening to the sound?


sound(Y,Fs)

How about amplify the signal?

sound(5*Y,Fs)

Now, it’s time for us to create new kind of sound. Let us create a sound from lower amplitude to higher one
and turn down back to lower amplitude.

J = [Y; 4*Y; 8*Y; 3*Y; Y];


sound(J, Fs)

And save the sound in a new file.

wavwrite(J, Fs, 'harimaukuat.wav');

© Anuar Mikdad Muad – JKEES, Universiti Kebangsaan Malaysia


MATLAB: DATA INPUT OUTPUT 7

IMAGE FILE

Reading an image file.

I = imread('logoUKM.bmp');

Display the image.

imshow(I)

© Anuar Mikdad Muad – JKEES, Universiti Kebangsaan Malaysia


MATLAB: DATA INPUT OUTPUT 8

VIDEO DATA

We’ve have seen the process of reading an image file. Here, we are going to make a video data out of
sequence of image data.

circle01.bmp circle02.bmp circle03.bmp circle04.bmp circle06.bmp

sequence

Load a number of image files.

I{1} = imread('circle01.bmp');
I{2} = imread('circle02.bmp');
I{3} = imread('circle03.bmp');
I{4} = imread('circle04.bmp');
I{5} = imread('circle05.bmp');
I{6} = imread('circle06.bmp');

Create a video file


aviobj = avifile('bulat.avi');

Compile the separate image files into an AVI file.


for k = 1:6
h = imshow(I{k});
set(h, 'EraseMode', 'xor');
axis equal;
frame = getframe(gca);
fsfd aviobj = addframe(aviobj, frame);
end

Close the file properly.

aviobj = close(aviobj);

© Anuar Mikdad Muad – JKEES, Universiti Kebangsaan Malaysia


MATLAB: DATA INPUT OUTPUT 9

BASIC STATISTICAL ANALYSIS

Let’s assume that the monthly temperature (in Celcius) of three cities was recorded and saved in a
‘temperature.txt’ file.

Load the temperature data from the file

temps = load('temperature.txt');
plot(temps)
xlabel('Months')
ylabel('Celcius')
title('Monthly Temperatures')

Find the average temperature…

avg temp = mean(temps)

avg_temp =

12.3333 8.4167 20.0000

Now, find the average for overall three cities.

avg avg = mean(avg temp)

avg_avg =

13.5833

© Anuar Mikdad Muad – JKEES, Universiti Kebangsaan Malaysia


MATLAB: DATA INPUT OUTPUT 10

Compute average for the second column.

avg temp = mean(temps(:,2))

avg_temp =

8.4167

We also can find their standard deviation

std temp = std(temps)

std_temp =

1.9695 2.2747 2.1320

Find the maximum temperature for each city.

max temp = max(temps)

max_temp =

15 12 23

Or, minimum temperature.


min temp = min(temps)

min_temp =

8 5 17

© Anuar Mikdad Muad – JKEES, Universiti Kebangsaan Malaysia


MATLAB: DATA INPUT OUTPUT 11

SORTING DATA

Type any numerical values not in their ascending order.

data = [3 2 4 5 2 5 7 2 9 10 1 3 1 1]'

Then, MATLAB will gives you this…

data =

3
2
4
5
2
5
7
2
9
10
1
3
1
1

Now, we would like to arrange the number in the ascending order. In MATLAB Workspace, type this…
sorted data = sort(data)

So we’ll get….

sorted_data =

1
1
1
2
2
2
3
3
4
5
5
7
9
10

© Anuar Mikdad Muad – JKEES, Universiti Kebangsaan Malaysia