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

# MATLAB: DATA INPUT OUTPUT 1

ASCII 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
.

## 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

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.

## 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

MATLAB: DATA INPUT OUTPUT 3

Saving Data

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).

## 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.

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

fwrite Write binary data
fgets/fgetl Read text strings from a file, line by line
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
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

MATLAB: DATA INPUT OUTPUT 5

## % ====== 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

satellite

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.

satellite(4)

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'

MATLAB: DATA INPUT OUTPUT 6

AUDIO DATA

## Where Y = data is stored in variable, Y

Fs = sampling frequency, (in Hertz)

figure, plot(Y)

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.

sound(J, Fs)

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

MATLAB: DATA INPUT OUTPUT 7

IMAGE FILE

## Display the image.

imshow(I)

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.

sequence

## 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);
end

## Close the file properly.

aviobj = close(aviobj);

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

plot(temps)
xlabel('Months')
ylabel('Celcius')
title('Monthly Temperatures')

avg_temp =

## avg avg = mean(avg temp)

avg_avg =

13.5833

MATLAB: DATA INPUT OUTPUT 10

avg_temp =

8.4167

std_temp =

max_temp =

15 12 23

## Or, minimum temperature.

min temp = min(temps)

min_temp =

8 5 17

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