Académique Documents
Professionnel Documents
Culture Documents
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');
>> 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
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.
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
Saving Data
A = magic(3)
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).
If we look at the folder that the file is being saved, we can see a newly created file. Open the file.
MAT FILE
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
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++.
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
x = 1;
while 1
satellite
satellite =
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 =
AUDIO DATA
[Y,Fs] = wavread('harimau.wav');
figure, plot(Y)
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.
IMAGE FILE
I = imread('logoUKM.bmp');
imshow(I)
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
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');
aviobj = close(aviobj);
Let’s assume that the monthly temperature (in Celcius) of three cities was recorded and saved in a
‘temperature.txt’ file.
temps = load('temperature.txt');
plot(temps)
xlabel('Months')
ylabel('Celcius')
title('Monthly Temperatures')
avg_temp =
avg_avg =
13.5833
avg_temp =
8.4167
std_temp =
max_temp =
15 12 23
min_temp =
8 5 17
SORTING DATA
data = [3 2 4 5 2 5 7 2 9 10 1 3 1 1]'
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