正式上线

非常重要的注意事项

matters need attention

一. Low-level File I/O Functions

Function     Description

fopen         Open file, or obtain information                             about  open files

fclose         Close one or all open files

fscanf         Read data from text file

fprintf          Write data to text file

feof             Test for end-of file

Open and close a file:

fid = fopen('[filename]','[permission]');

status = fclose(fid);

'[Permission]' 当中有着很多钟形态: ‘r+' 'r' 'w' 'w+' 'a'

这里既可以读,也可以写,学过C语言的朋友会发现其实这里很像C语言当中对于文件的操作,其实也无需感到太过惊异,因为MATLAB本身也就是基于C语言而开发出来的一款软件

Writing Sine values into A file

Generate x,y

Open a file

Write x, y into the file

Close the file.

接下来提供一个画出sin wave 的范例程式:

x=0: pi/10:pi;  y = sin(x);   fid = fopen('sinx.txt','w')

for i = 1:11

fprintf(fid, '%5.3f  %8.4f\n' , x(i), y(i));

fclose(fid); type sinx.txt

这个程式首先产生X ,产生y

二.Read and Write through Formatted

Read: A = fscanf(fid, format ,size);

其中A表示被阅读的数据, format,用于控制与    限定指定的阅读格式,size则表示需要阅读的     数据的量。

Write: fprintf(fid, format, x,y,....);

Format specifier:%-12.5e ,(其中12.5用来控  制整个数据格式的宽度与精度。

Specifier Description

%c  Single character   %o Octal notation(unsigned)

%d Decimal notation(signed)  %s String of characters

%e Exponential notation %u Decimal notation (unsigned)

%f Fixed-point notation %x Hexadecimal notation

%g The more compact of %e or %f

PS(这里穿插一些小知识):

数学建模中有时会利用MATLAB进行属性值的归一化处理:

若属性值为效益型,则令:

Rij =  aij/max aij

或是:rij = aij-min(i)aij/max(i) aij - min(i) aij

若属性值为成本型,则令:

rij = min(i)aij/aij

或rij = max(i)aij - aij /max aij - min aij

若属性值为固定型,则令

rij = 1 - aij - arf j /max|aij - arfj|

若属性值为偏离型,则令 rij = |aij - bt j|- min|aij-btj| /max|aij - btj|-min|aij-btj|

若属性值为区间型,则令:

rij = { 1 - max(q1^j - aij,aij - q2^j)/max(q1^j - in aij ,max aij- -q2^j)  (aij 不属于[q1j,q2j])

1  aij属于[q1^j,q2^j]

若属性值为偏离区间型,则令rij = max(q1j - aij, aij-q2j)/max(q1j- minaij , max aij- q2^j) (aij 不属于【q1j , q2j]

0     aij 属于[q1^j, q2^j]

Reading from files

Check if it is the end of the file:feof(fid)

04asciiData.txt:  John 1995 12 5 12.3 3.24

Tom  1995 12 7 2.3  2.0

Jean 1996 3 2  10.2 0

fid = fopen(’ascillData.txt','r'); i = 1;

while~feof(fid)

name(i,:) = fscanf(fid,‘%5c',1);

year(i) = fscanf(fid,'%d',1);

no1(i) = fscanf(fid,'%d,1);

no2(i) = fscanf(fid,'%d',1);

no3(i) = fscanf(fid,'%g',1);

no4(i) = fscanf(fid,'%g',1);

fclose(fid);

接下来的阶段呢,大家要学习的主要是

(1)Basic plotting

(2)  Graphical object properties

Matlab has a powerful plotting engine that can generate a wide variety of plots

Plot from "Data"

MATLAB does not understand functions

f(t)= sin(2派t)

Strategies:

1.Generate the numeric values of a function over a specific range

2.Display the data"points" in a graphical way

接下来向大家介绍人们在画图过程中最常使用的指令:plot()