Skip to content Skip to navigation

Thu vien Hoc Lieu Mo Viet Nam

You are here: Home » Content » Tạo giao diện trong Matlab

Navigation

Recently Viewed

This feature requires Javascript to be enabled.

Tạo giao diện trong Matlab

Module by: ThS.Nguyễn Chí Ngôn. E-mail the author

User rating (How does the rating system work?)
Ratings

Ratings allow you to judge the quality of modules. If other users have ranked the module then its average rating is displayed below. Ratings are calculated on a scale from one star (Poor) to five stars (Excellent).

How to rate a module

Hover over the star that corresponds to the rating you wish to assign. Click on the star to add your rating. Your rating should be based on the quality of the content. You must have an account and be logged in to rate content.

:
(0 ratings)

Summary: Tạo giao diện trong Matlab

TẠO GIAO DIỆN TRONG MATLAB

MỤC TIÊU

Bài thí nghiệm này trang bị cho sinh viên một số kiến thức cơ bản để xây dựng giao diện người dùng trong môi trường Matlab, nhằm hoàn thiện một chương trình ứng dụng nhất định.

THAM KHẢO

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

[2]. 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.

[3]. Nguyễn Hoài Sơn - Đỗ Thanh Việt - Bùi Xuân Lâm, Ứng dụng MATLAB trong tính toán kỹ thuật, Tập 1, NXB ĐHQG Tp. HCM, 2000 (trang 328-344).

THỰC HÀNH

Cũng như các ngôn ngữ cấp cao khác, Matlab hổ trợ nhiều công cụ chức năng cho phép lập trình tạo giao diện sử dụng đẹp và nhanh chóng. Ví dụ, các dạng nút ấn, cửa sổ soạn thảo, các dạng menu, … như hình 6.1.

Hình 1: Hình 6.1 – Các công cụ hổ trợ giao diện
Hình 1 (graphics1.jpg)

Trong bài thí nghiệm này, sinh viên lần lượt thực hiện các bước được nêu ra để được một giao diện đơn giản như hình vẽ 6.2. Trong mỗi bước thực hành, sinh viên hãy quan sát kỹ đáp ứng của chương trình, từ đó tự mình rút ra kinh nghiệm về việc điều chỉnh các thuộc tính đồ hoạ của Matlab.

1. Tạo cửa sổ chính figure – ‘Welcome to User Interface’s Giude’ theo cách sau:

  • Mở Matlab Editor
  • Nhập nội dung sau:

% User Interface's Guide

%

% Matlab Experiments 2003

% TcAD, CIT, Cantho University

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

% Initialize whole figure...

namefig = 'Welcome to User Interface''s Guide';

figpos = get(0,'DefaultFigurePosition'); % lay vi tri mac nhien

figpos(1)= figpos(1)-10; figpos(2)= figpos(2)-10;

figpos(3)= figpos(3)+10; figpos(4)= figpos(4)+10;

% Tao figure

fig=figure( ...

'Name', namefig, ...

'NumberTitle','off', ...

'Position',figpos);

Hình 2: Hình 6.2 – Một giao diện đơn giản
Hình 2 (graphics2.jpg)

  • Lưu thành file giaodien.m
  • >>giaodien <> % thi hanh

Quan sát kết quả trên hình 6.3 (Lưu ý các thuộc tính: Name, Position)

Hình 3: (Hình 6.3)
Hình 3 (graphics3.jpg)
  • >>set(fig) % xem thuộc tính của figure trong cửa sổ lệnh

2. Thêm vào figure một axes cho phép hiển thị đồ họa:

  • Nhập tiếp nội dung sau vào file giaodien.m

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

% main axes

axs=axes('Position',[0.05 0.4 0.65 0.55]);

  • Lưu và thi hành file này.
Hình 4: (Hình 6.4)
Hình 4 (graphics4.jpg)

3. Hiển thị dòng ‘Matlab Experiments’ bên dưới axes:

  • Nhập tiếp nội dung sau vào file giaodien.m

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

% text

txtpos=[10 50 425 50];

txt=uicontrol(...

'Style','text',...

'BackgroundColor',[0.8 0.8 0.8],...

'ForegroundColor',[0.4 0.5 0.3],...

'String','Matlab Experiments',...

'Position',txtpos,...

'Fontname','Courier',...

'FontWeight','Bold',...

'FontSize',26);

  • Lưu và thi hành file này.

Hình 5: (Hình 6.5)
Hình 5 (graphics5.jpg)

4. Tạo một frame có shadow bên phải figure để đặt các nút chức năng:

  • Nhập tiếp nội dung sau vào file giaodien.m

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

% Console frames

p1=0.755; p2=0.05; p3=0.2; p4=0.90;

frm1pos = [p1 p2 p3 p4];

frm2pos = [p1-0.005 p2+0.005 p3 p4];

% shadow frame

frm1=uicontrol( ...

'Style','frame', ...

'Units','normalized', ...

'Position',frm1pos, ...

'ForegroundColor',[0.4 0.4 0.4],...

'BackgroundColor',[0.4 0.4 0.4]);

% main frame

frm2=uicontrol( ...

'Style','frame', ...

'Units','normalized', ...

'Position',frm2pos, ...

'ForegroundColor',[0.7 0.7 0.7],...

'BackgroundColor',[0.65 0.65 0.65]);

  • Lưu và thi hành file này.
Hình 6: (Hình 6.6)
Hình 6 (graphics6.jpg)

5. Tạo nút ‘Close’ có chức năng đóng cửa sổ figure hiện hành:

  • Nhập tiếp nội dung sau vào file giaodien.m

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

% Close button

closeHndl=uicontrol(...

'Style','pushbutton', ...

'Units','normalized', ...

'Position',[p1+0.01 p2+0.05 p3-0.025 0.05], ...

'String','Close', ...

'Foregroundcolor','b',...

'Fontsize',9,...

'Callback','close');

  • Lưu và thi hành file này.

Vấn đề quan trọng nhất đối với một nút chức năng là thi hành công việc tương ứng khi người sử dụng thao tác. Thuộc tính ‘CallBack’ cho phép: thi hành một lệnh của Matlab dưới dạng chuỗi (bao gồm lệnh gọi hàm, script file, biểu thức toán, …). Sinh viên thử thay lệnh close bằng một lệnh khác, chẳng hạn demos và quan sát đáp ứng.

6. Tạo nút Sphere để vẽ một hình cầu 3D:

  • Nhập tiếp nội dung sau vào file giaodien.m

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

% Sphere button

sph=uicontrol( ...

'Style','pushbutton', ...

'Units','normalized', ...

'Position',[p1+0.01 p4-0.05 p3-0.025 0.05], ...

'String','Sphere', ...

'Foregroundcolor','b',...

'Fontsize',9,...

'Callback',['[x,y,z]=sphere(35);', 'surfl(x,y,z);', 'axis equal']);

  • Lưu và thi hành file này.
Hình 7: (Hình 6.7)
Hình 7 (graphics7.jpg)

7. Tạo nút Picture để hiển thị ảnh màu:

  • Nhập tiếp nội dung sau vào file giaodien.m

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

% Picture button

pic=uicontrol( ...

'Style','pushbutton', ...

'Units','normalized', ...

'Position',[p1+0.01 p4-0.125 p3-0.025 0.05], ...

'String','Picture', ...

'Foregroundcolor','b',...

'Fontsize',9,...

'Callback',['imshow(imread(''flowers.tif''))']);

  • Lưu và thi hành file này.
Hình 8: (Hình 6.8)
Hình 8 (graphics8.jpg)

Sinh viên lưu ý cách biểu diễn nhiều lệnh ở dạng chuỗi cho ‘CallBack’. Trong trường hợp có quá nhiều lệnh phục vụ chức năng này, ta nên đưa chúng vào một script file hoặc một hàm khác. Ngoài ra, nếu ta tạo giao diện dưới dạng một hàm (function) thì thuộc tính ‘CallBack’ cho phép gọi một hàm con được viết ngay trong file này.

8. Sinh viên hãy tạo thêm nút ‘About’ sao cho khi người sử dụng ấn nút sẽ mở cửa sổ mới nằm ngay giữa màn hình 6.9.

Hình 9: (Hình 6.9)
Hình 9 (graphics9.jpg)

TỰ CHỌN

Sinh viên hãy xem một số file tạo giao diện demo của các toolbox trong Matlab chẳng hạn: graf3d.m trong \toolbox\matlab\demos, để tham khảo cách xây dựng giao diện theo dạng hàm. Với cách này, thuộc tính ‘CallBack’ trong ‘uicontrol’ cho phép gọi trực tiếp một hàm mà hàm đó được xây dựng ngay trong file giao diện (nghĩa là không cần tạo một file *.m khác phục vụ cho ‘CallBack’).

Thử sửa lại giao diện của bài thí nghiệm này theo dạng trên.

Content actions

Give Feedback:

E-mail the module author | Rate module ( How does the rating system work?)

Rating system

Ratings

Ratings allow you to judge the quality of modules. If other users have ranked the module then its average rating is displayed below. Ratings are calculated on a scale from one star (Poor) to five stars (Excellent).

How to rate a module

Hover over the star that corresponds to the rating you wish to assign. Click on the star to add your rating. Your rating should be based on the quality of the content. You must have an account and be logged in to rate content.

(0 ratings)

Download:

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks