1. 实现功能
实现输入x中数据输入,点击输出,然后将输入框中的值通过setappdata/getappdata/rmappdata的方式传递和共享回调函数之间的数据。
2. 实现界面
3. 代码
【mygui.m】
function mygui
%% 清除变量,清命令窗口屏,清除图形
clear;
clc;
%% 新建图形并设置相关属性
%scrsz = get(0,'ScreenSize');
Hd_fig = figure('NumberTitle', 'off', 'Resize','off','Color',[0.753,0.753,0.753],'Name', 'BP神经网络预测系统');
%取得Figure的JavaFrame。
%修改图标
newIcon = javax.swing.ImageIcon('icon.png');
figFrame = get(Hd_fig,'JavaFrame');
figFrame.setFigureIcon(newIcon);
%转换图形窗口菜单条的“开”与“关”
set(Hd_fig,'menubar','none','Unit','normalized','Position',[0.08,0.12,0.78,0.74]);
%% 初始化图形中控件
%坐标图
axes('Position',[0.06,0.095,0.7,0.85]);
title('BP神经网络预测输出','FontName','Times New Roman','FontWeight','Bold','FontSize',10);
xlabel('横坐标','FontName','Times New Roman','FontSize',14);
ylabel('纵坐标','FontName','Times New Roman','FontSize',14);
%% 控制按钮
Hd_p1 = uipanel('Parent',Hd_fig,'Title','控制功能',...
'BorderType','etchedin',...
'BackgroundColor','white',...
'ShadowColor',[0.4,0.4,0.4],'TitlePosition','centertop','tag','Hd_p1',...
'ForegroundColor','b',...
'FontWeight','bold',...
'position',[0.77,0.094,0.22,0.09]);
Hd_load = uicontrol('Parent',Hd_p1,'Style','Push',...
'String','导入数据',...
'Units','normalized',...
'FontWeight','bold',...
'ForegroundColor','black',...
'Units','normalized',...
'FontSize',10,...
'tag','Hd_load',...
'Position',[0.03,0.07,0.45,0.98],...
'CallBack','');
Hd_close = uicontrol('Parent',Hd_p1,'Style','Push',...
'String','退出',...
'FontWeight','bold',...
'ForegroundColor','r',...
'Units','normalized',...
'FontSize',10,...
'tag','Hd_close',...
'Position',[0.52,0.07,0.45,0.98],...
'CallBack','close');
%% 数据处理
Hd_p2 = uipanel('Parent',Hd_fig,'Title','数据处理',...
'BorderType','etchedin',...
'BackgroundColor','white',...
'ShadowColor',[0.4,0.4,0.4],'TitlePosition','centertop','tag','Hd_p1',...
'ForegroundColor','b',...
'FontWeight','bold',...
'position',[0.77,0.19,0.22,0.20]);
%
Hd_b1 = uicontrol('Parent',Hd_p2,'Style','Push',...
'String','相关性分析',...
'Units','normalized',...
'FontWeight','bold',...
'ForegroundColor','black',...
'Units','normalized',...
'FontSize',10,...
'tag','Hd_b1',...
'Position',[0.03,0.028,0.45,0.3],...
'CallBack','in=getappdata(gcf,''in'')');
Hd_b2 = uicontrol('Parent',Hd_p2,'Style','Push',...
'String','聚类',...
'FontWeight','bold',...
'ForegroundColor','black',...
'Units','normalized',...
'FontSize',10,...
'tag','Hd_b2',...
'Position',[0.52,0.028,0.45,0.3],...
'CallBack','');
%
Hd_b3 = uicontrol('Parent',Hd_p2,'Style','Push',...
'String','BP训练',...
'Units','normalized',...
'FontWeight','bold',...
'ForegroundColor','black',...
'Units','normalized',...
'FontSize',10,...
'tag','Hd_b3',...
'Position',[0.03,0.36,0.45,0.3],...
'CallBack','');
Hd_b4 = uicontrol('Parent',Hd_p2,'Style','Push',...
'String','BP误差',...
'FontWeight','bold',...
'ForegroundColor','black',...
'Units','normalized',...
'FontSize',10,...
'tag','Hd_b4',...
'Position',[0.52,0.36,0.45,0.3],...
'CallBack','');
%
Hd_b5 = uicontrol('Parent',Hd_p2,'Style','Push',...
'String','SVM训练',...
'Units','normalized',...
'FontWeight','bold',...
'ForegroundColor','black',...
'Units','normalized',...
'FontSize',10,...
'tag','Hd_b5',...
'Position',[0.03,0.69,0.45,0.3],...
'CallBack','');
Hd_b6 = uicontrol('Parent',Hd_p2,'Style','Push',...
'String','SVM误差',...
'FontWeight','bold',...
'ForegroundColor','black',...
'Units','normalized',...
'FontSize',10,...
'tag','Hd_b6',...
'Position',[0.52,0.69,0.45,0.3],...
'CallBack','');
%% 输入变量
Hd_p3 = uipanel('Parent',Hd_fig,'Title','Input',...
'BorderType','etchedin',...
'BackgroundColor','white',...
'ShadowColor',[0.4,0.4,0.4],'TitlePosition','centertop','tag','Hd_p1',...
'ForegroundColor','b',...
'FontWeight','bold',...
'position',[0.77,0.4,0.22,0.45]);
%
Hd_b11 = uicontrol('Parent',Hd_p3,'Style','edit',...
'String','输入1',...
'Units','normalized',...
'FontWeight','bold',...
'ForegroundColor','black',...
'Units','normalized',...
'FontSize',10,...
'Enable','inactive',...
'Position',[0.03,0.015,0.45,0.15],...
'CallBack','');
Hd_b12 = uicontrol('Parent',Hd_p3,'Style','Edit',...
'String','0.0',...
'FontWeight','bold',...
'ForegroundColor',[0.871,0.49,0],...
'Units','normalized',...
'FontSize',10,...
'tag','Hd_b12',...
'Position',[0.52,0.015,0.45,0.15],...
'CallBack','');
%
Hd_b21 = uicontrol('Parent',Hd_p3,'Style','edit',...
'String','输入2',...
'Units','normalized',...
'FontWeight','bold',...
'ForegroundColor','black',...
'Units','normalized',...
'FontSize',10,...
'Enable','inactive',...
'Position',[0.03,0.182,0.45,0.15],...
'CallBack','');
Hd_b22 = uicontrol('Parent',Hd_p3,'Style','Edit',...
'String','0.0',...
'FontWeight','bold',...
'ForegroundColor',[0.871,0.49,0],...
'Units','normalized',...
'FontSize',10,...
'tag','Hd_b22',...
'Position',[0.52,0.182,0.45,0.15],...
'CallBack','');
%
Hd_b31 = uicontrol('Parent',Hd_p3,'Style','edit',...
'String','输入3',...
'Units','normalized',...
'FontWeight','bold',...
'ForegroundColor','black',...
'Units','normalized',...
'FontSize',10,...
'Enable','inactive',...
'Position',[0.03,0.349,0.45,0.15],...
'CallBack','');
Hd_b32 = uicontrol('Parent',Hd_p3,'Style','Edit',...
'String','0.0',...
'FontWeight','bold',...
'ForegroundColor',[0.871,0.49,0],...
'Units','normalized',...
'FontSize',10,...
'tag','Hd_b32',...
'Position',[0.52,0.349,0.45,0.15],...
'CallBack','');
%
Hd_b41 = uicontrol('Parent',Hd_p3,'Style','edit',...
'String','输入4',...
'Units','normalized',...
'FontWeight','bold',...
'ForegroundColor','black',...
'Units','normalized',...
'FontSize',10,...
'Enable','inactive',...
'Position',[0.03,0.516,0.45,0.15],...
'CallBack','');
Hd_b42 = uicontrol('Parent',Hd_p3,'Style','Edit',...
'String','0.0',...
'FontWeight','bold',...
'ForegroundColor',[0.871,0.49,0],...
'Units','normalized',...
'FontSize',10,...
'tag','Hd_b42',...
'Position',[0.52,0.516,0.45,0.15],...
'CallBack','');
%
Hd_b51 = uicontrol('Parent',Hd_p3,'Style','edit',...
'String','输入5',...
'Units','normalized',...
'FontWeight','bold',...
'ForegroundColor','black',...
'Units','normalized',...
'FontSize',10,...
'Enable','inactive',...
'Position',[0.03,0.683,0.45,0.15],...
'CallBack','');
Hd_b52 = uicontrol('Parent',Hd_p3,'Style','Edit',...
'String','0.0',...
'FontWeight','bold',...
'ForegroundColor',[0.871,0.49,0],...
'Units','normalized',...
'FontSize',10,...
'tag','Hd_b52',...
'Position',[0.52,0.683,0.45,0.15],...
'CallBack','');
%
Hd_b61 = uicontrol('Parent',Hd_p3,'Style','edit',...
'String','输入6',...
'Units','normalized',...
'FontWeight','bold',...
'ForegroundColor','black',...
'Units','normalized',...
'FontSize',10,...
'Enable','inactive',...
'Position',[0.03,0.85,0.45,0.15],...
'CallBack','');
Hd_b62 = uicontrol('Parent',Hd_p3,'Style','Edit',...
'String','0.0',...
'FontWeight','bold',...
'ForegroundColor',[0.871,0.49,0],...
'Units','normalized',...
'FontSize',10,...
'tag','Hd_b62',...
'Position',[0.52,0.85,0.45,0.15],...
'CallBack','');
%% 输出
Hd_p4 = uipanel('Parent',Hd_fig,'Title','Output',...
'BorderType','etchedin',...
'BackgroundColor','white',...
'ShadowColor',[0.4,0.4,0.4],'TitlePosition','centertop','tag','Hd_p1',...
'ForegroundColor','b',...
'FontWeight','bold',...
'position',[0.77,0.865,0.22,0.1]);
%
Hd_o1 = uicontrol('Parent',Hd_p4,'Style','push',...
'String','输出',...
'Units','normalized',...
'FontWeight','bold',...
'ForegroundColor','black',...
'Units','normalized',...
'FontSize',10,...
'Position',[0.03,0.1,0.45,0.9],...
'CallBack','getInputData');
Hd_o2 = uicontrol('Parent',Hd_p4,'Style','Edit',...
'String','',...
'FontWeight','bold',...
'ForegroundColor',[0,0,1],...
'Units','normalized',...
'FontSize',10,...
'tag','Hd_o2',...
'Position',[0.52,0.1,0.45,0.9],...
'CallBack','');
【getInputData.m】
function getInputData
%默认四个变量,变量个数获取在此处修改
Hd_b12 = findobj(gcf,'tag','Hd_b12');
Hd_b22 = findobj(gcf,'tag','Hd_b22');
Hd_b32 = findobj(gcf,'tag','Hd_b32');
Hd_b42 = findobj(gcf,'tag','Hd_b42');
str1 = get(Hd_b12,'String')
str2 = get(Hd_b22,'String')
str3 = get(Hd_b32,'String')
str4 = get(Hd_b42,'String')
% str5 = get(Hd_b52,'String')
% str6 = get(Hd_b62,'String')
%字符转数字
in(1) = str2double(str1);
in(2) = str2double(str2);
in(3) = str2double(str3);
in(4) = str2double(str4);
%检测输入的值是否符合标准,符合则保存数据
if(isnan(in(1)) || isnan(in(2)) || isnan(in(3)) || isnan(in(4)))
msg = msgbox('Input Error!!!','Error');
%字体大小设置和居中
amsg = get(msg,'CurrentAxes');
chmsg = get(amsg,'Children');
set(chmsg,'FontSize',14);
th = findall(0,'Tag','MessageBox');
boxPosition = get(msg,'Position');
textPosition = get(th,'Position');
set(th,'position',[boxPosition(3).*0.5 textPosition(2) textPosition(3)]);
set(th, 'HorizontalAlignment', 'center');
%修改图标
newIcon = javax.swing.ImageIcon('error.png');
figMsg = get(msg,'JavaFrame');
figMsg.setFigureIcon(newIcon);
return;
else
in(:)
%处理数据传送
setappdata(gcf,'in',in);
end