<< Chapter < Page Chapter >> Page >
Đây là giáo trình về hàm và script files

Mục tiêu

Bài thí nghiệm này giúp sinh viên tiếp cận với kỹ thuật lập trình trong Matlab thông qua việc sử dụng các hàm có sẵn, xây dựng các hàm mới dựa trên các cấu trúc if - for - while và tìm hiểu cách xây dựng script files, làm cơ sở cho việc lập trình mô phỏng các hệ thống Viễn thông và Tự động ở các bài sau.

Tham khảo

[1].Nguyễn Hứa Duy Khang, Bài giảng môn CAD, Bộ môn Viễn thông&Tự động hóa, khoa Công nghệ thông tin, Đại học Cần thơ, 2001.

[2].The Mathworks Inc., Matlab Notebook User’s Guide, 2003.

[3].Nguyễn Công Định, Phân tích và tổng hợp các hệ thống điều khiển bằng máy tính, NXB Khoa học và Kỹ thuật, 2002.

[4].Nguyễn Hữu Tình - Lê Tấn Hùng - Phạm Thị Ngọc Yến - Nguyễn Thị Lan Hương, Cơ sở Matlab&ứng dụng, NXB KH và Kỹ thuật, 1999.

[5]. (External Link)

[6]. (External Link)

Thực hành

Hàm và Script files trong Matlab đều được quản lý dưới dạng các tập tin có phần mở rộng .m, thường được soạn thảo bởi Matlab Editor. Khởi động Matlab Editor bằng một trong các cách sau:

  • Nhấp chuột vào biểu tượngtrên menu bar của cửa sổ lệnh.
  • File  New  M-file
  • Nhấp vào biểu tượng Matlab Editor trên desktop (nếu có).

Chúng ta lần lượt khảo sát qui cách xây dựng các hàm và script file. Sinh viên đọc và thực hành lần lượt theo các phần sau.

Script files

Tập hợp các dòng lệnh của Matlab được sắp xếp theo một cấu trúc nào đó và lưu thành file có phần mở rộng *.m được gọi là script file (file kịch bản, file chương trình). Ta có thể chạy file này từ cửa sổ lệnh giống hệt như các lệnh của Matlab. Cấu trúc của một script file như sau:

%------------------------------------------------------------------------------------------ %Phần viết sau dấu ‘%’ ở đây dùng cho lệnh help

%Thông thường phần này mô tả chức năng, cách sử dụng,

%ví dụ minh họa hay những lưu ý đặc biệt mà tác giả mong muốn trợ

% giúp cho người sử dụng.

%-----------------------------------------------------------------------------------------

[global tênbiến1, tênbiến2,… ]% Khai báo biến toàn cục % (nếu có)

<các câu lệnh>% phần trình bày câu lệnh

Hãy khởi động Matlab Editor và tạo một script file có tên bai21.m, với nội dung như sau:

% Doan script file nay hien thi loi chao trong 2s. Sau do

% hien thi logo cua matlab mot cach sinh dong roi thoat

close all

% ---------------------- Tao mot cua so do hoa --------------------------------

figure('Color',[0 0 0],...

'Name','Welcome to Matlab Experiments',...

'NumberTitle','off',...

'MenuBar','none');

% ----------------------- Hien thi loi chao ----------------------------------------

text('String','Welcome to MATLAB',...

'Color',[.25 .25 .25],...

'Position',[0.01 .501],...

'Fontsize',32,...

'FontAngle','italic');

text( 'String','Welcome to MATLAB',...

'Color','w',...

'Position',[0 .5],...

'Fontsize',32,...

'FontAngle','italic');

axis off;

pause(2); % dung trong 2 giay

% ---------------------- Hien thi logo cua Matlab ------------------------------

logospin

% ---- Thoat - xoa cac bien trong workspace va dong cua so lai -----

clear

close

% ket thuc script file

Sau khi lưu file này, từ cửa sổ lệnh của Matlab, sinh viên hãy nhập:

Questions & Answers

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Thí nghiệm cad (computer-aided design). OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10797/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Thí nghiệm cad (computer-aided design)' conversation and receive update notifications?

Ask