
Тема7_Matlab
.pdf
Тема 7
Контрольное задание
Разработайте приложение с графическим интерфейсом, с помощью которого можно выполнить следующие операции:
•Ввести исходные данные из файла PRIM_GUI.txt (4 параметра: ФИО сотрудника, его основная зарплата, надбавка, премия).
•Обеспечить на экранной форме выбор одного из сотрудников, представленных в файле и ввод ставки налога.
•Для выбранного сотрудника вывести на экранной форме его ФИО и отдельно 3 компоненты оплаты его труда, а также суммарное их значение и доход за вычетом налога.
1.Главная программа KADRY.m:
global NOM_POL FIO ZP1 NADB1 Prem1 ff=fopen('PRIM_GUI1.txt'); bb=fscanf(ff,'%c');
fclose(ff);
Z=reshape(bb,37,[])';
FIO=Z(:,1:15);
ZP=Z(:,16:22);
NADB=Z(:,23:28);
Prem=Z(:,29:36);
n=size(Z,1); for ii=1:n
ZP1(ii)=str2num(ZP(ii,:));
NADB1(ii)=str2num(NADB(ii,:));
Prem1(ii)=str2num(Prem(ii,:)); end
NOM_POL=1; f=openfig('KADRY_form.fig','reuse');
2.Форма KADRY_form.fig:
1
3.Программа KADRY_form.m, обслуживающая форму (файл создается автоматически при создании формы; добавленный код выделен желтым):
function varargout = KADRY_form(varargin)
%KADRY_FORM M-file for KADRY_form.fig
%KADRY_FORM, by itself, creates a new KADRY_FORM or raises the existing
%singleton*.
%
%H = KADRY_FORM returns the handle to a new KADRY_FORM or the handle to
%the existing singleton*.
%
%KADRY_FORM('CALLBACK',hObject,eventData,handles,...) calls the local
%function named CALLBACK in KADRY_FORM.M with the given input
arguments.
%KADRY_FORM('Property','Value',...) creates a new KADRY_FORM or raises the
%existing singleton*. Starting from the left, property value pairs are
%applied to the GUI before KADRY_form_OpeningFunction gets called. An
%unrecognized property name or invalid value makes property application
%stop. All inputs are passed to KADRY_form_OpeningFcn via varargin.
%
%*See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
%instance to run (singleton)".
%
%See also: GUIDE, GUIDATA, GUIHANDLES
%Copyright 2002-2003 The MathWorks, Inc.
%Edit the above text to modify the response to help KADRY_form
%Last Modified by GUIDE v2.5 21-Apr-2020 18:06:20
%Begin initialization code - DO NOT EDIT
gui_Singleton = 1; |
|
gui_State = struct('gui_Name', |
mfilename, ... |
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @KADRY_form_OpeningFcn, ...
'gui_OutputFcn', @KADRY_form_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else
gui_mainfcn(gui_State, varargin{:}); end
%End initialization code - DO NOT EDIT
%--- Executes just before KADRY_form is made visible.
function KADRY_form_OpeningFcn(hObject, eventdata, handles, varargin)
2

% This function has no output args, see OutputFcn. % hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
%varargin command line arguments to KADRY_form (see VARARGIN)
%Choose default command line output for KADRY_form
handles.output = hObject;
%Update handles structure guidata(hObject, handles);
%UIWAIT makes KADRY_form wait for user response (see UIRESUME)
%uiwait(handles.figure1);
%--- Outputs from this function are returned to the command line.
function varargout = KADRY_form_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
%Get default command line output from handles structure varargout{1} = handles.output;
%--- Executes on selection change in popupmenu1. function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
%Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
%contents{get(hObject,'Value')} returns selected item from popupmenu1 global NOM_POL FIO ZP1 NADB1 Prem1 NOM_POL=get(hObject,'value');
handles=guihandles(gcbo); Dohod=ZP1(NOM_POL)+NADB1(NOM_POL)+Prem1(NOM_POL); Nalog=str2num(get(handles.edit1,'string')) set(handles.text2,'string',['Зарплата = ' num2str(ZP1(NOM_POL))]); set(handles.text3,'string',['Надбавка = ' num2str(NADB1(NOM_POL))]); set(handles.text4,'string',['Премия = ' num2str(Prem1(NOM_POL))]); set(handles.text5,'string',['Общий доход = ' num2str(Dohod)]); set(handles.text7,'string',['Доход после уплаты налога = ' num2str(Dohod-
Dohod*Nalog/100)]);
% --- Executes during object creation, after setting all properties. function popupmenu1_CreateFcn(hObject, eventdata, handles) % hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
3

%See ISPC and COMPUTER.
if ispc set(hObject,'BackgroundColor','white');
else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
global NOM_POL FIO ZP1 NADB1 Prem1 set(hObject,'value',NOM_POL,'string',FIO); handles=guihandles(gcbo); Dohod=ZP1(NOM_POL)+NADB1(NOM_POL)+Prem1(NOM_POL); set(handles.text2,'string',['Зарплата = ' num2str(ZP1(NOM_POL))]);
set(handles.text3,'string',['Надбавка = ' num2str(NADB1(NOM_POL))]); set(handles.text4,'string',['Премия = ' num2str(Prem1(NOM_POL))]); set(handles.text5,'string',['Общий доход = ' num2str(Dohod)]);
%--- Executes during object creation, after setting all properties.
function text2_CreateFcn(hObject, eventdata, handles) % hObject handle to text2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
global NOM_POL ZP1
set(hObject,'string',['Зарплата =' num2str(ZP1(NOM_POL))])
% --- Executes during object creation, after setting all properties. function text3_CreateFcn(hObject, eventdata, handles)
% hObject handle to text3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
global NOM_POL NADB1
set(hObject,'string',['Надбавка =' num2str(NADB1(NOM_POL))])
% --- Executes during object creation, after setting all properties. function text4_CreateFcn(hObject, eventdata, handles)
% hObject handle to text4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
global NOM_POL Prem1
set(hObject,'string',['Премия =' num2str(Prem1(NOM_POL))])
% --- Executes during object creation, after setting all properties. function text5_CreateFcn(hObject, eventdata, handles)
% hObject handle to text5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called global NOM_POL ZP1 NADB1 Prem1
set(hObject,'string',['Общий доход =' num2str(ZP1(NOM_POL)+NADB1(NOM_POL)+Prem1(NOM_POL))])
function edit1_Callback(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO)
4

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
%Hints: get(hObject,'String') returns contents of edit1 as text
%str2double(get(hObject,'String')) returns contents of edit1 as a double
%--- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
%Hint: edit controls usually have a white background on Windows.
%See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white'); end
Пример ИКЗ
Разработать программу с ГИП, выполняющую операции:
1.Запрос на форме имени текстового файла.
2.Ввод матрицы из выбранного файла.
3.Отобразить на форме размерности введенной матрицы.
4.Подсчитать сумму элементов матрицы.
5.Отобразить на форме результат расчета.
1.Главная программа T7.m: run('matr.m')
2.Экранная форма matr.fig:
5
3.Программа matr.m, обслуживающая экранную форму (файл создается автоматически при создании формы; добавленный код выделен желтым):
function varargout = matr(varargin)
%MATR MATLAB code for matr.fig
%MATR, by itself, creates a new MATR or raises the existing
%singleton*.
%
%H = MATR returns the handle to a new MATR or the handle to
%the existing singleton*.
%MATR('CALLBACK',hObject,eventData,handles,...) calls the local
%function named CALLBACK in MATR.M with the given input arguments.
%MATR('Property','Value',...) creates a new MATR or raises the
%existing singleton*. Starting from the left, property value pairs are
%applied to the GUI before matr_OpeningFcn gets called. An
%unrecognized property name or invalid value makes property application
%stop. All inputs are passed to matr_OpeningFcn via varargin.
%
%*See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
%instance to run (singleton)".
%
%See also: GUIDE, GUIDATA, GUIHANDLES
%Edit the above text to modify the response to help matr
%Last Modified by GUIDE v2.5 22-Apr-2020 10:58:25
%Begin initialization code - DO NOT EDIT
gui_Singleton = 1; |
|
gui_State = struct('gui_Name', |
mfilename, ... |
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @matr_OpeningFcn, ...
'gui_OutputFcn', @matr_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else
gui_mainfcn(gui_State, varargin{:}); end
%End initialization code - DO NOT EDIT
%--- Executes just before matr is made visible.
function matr_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn.
% hObject handle to figure
6

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
%varargin command line arguments to matr (see VARARGIN)
%Choose default command line output for matr
handles.output = hObject;
%Update handles structure guidata(hObject, handles);
%UIWAIT makes matr wait for user response (see UIRESUME)
%uiwait(handles.figure1);
%--- Outputs from this function are returned to the command line. function varargout = matr_OutputFcn(hObject, eventdata, handles)
%varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure varargout{1} = handles.output;
function edit1_Callback(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
%Hints: get(hObject,'String') returns contents of edit1 as text
%str2double(get(hObject,'String')) returns contents of edit1 as a double
fn=get(handles.edit1,'string') if exist(fn,'file')~=0
M=load(fn)
[m1,n1]=size(M)
s=sum(sum(M)) set(handles.text2,'string','')
set(handles.text3,'string',['Число строк = ' num2str(m1)]) set(handles.text4,'string',['Число столбцов = ' num2str(n1)]) set(handles.text5,'string',['Сумма элементов = ' num2str(s)])
else
set(handles.text2,'string','Файл не найден!') set(handles.text3,'string','') set(handles.text4,'string','') set(handles.text5,'string','')
end
% --- Executes during object creation, after setting all properties. function edit1_CreateFcn(hObject, eventdata, handles)
7
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
%Hint: edit controls usually have a white background on Windows.
%See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');
end
8