基于MATLAB APPDesign 开发的MySQL数据库及数据表显示APP

上一篇:如何将Mysql与MATLAB连接

1、效果图

        先看APP整体效果

2、开发流程

2.1 基本控件介绍

2.1.1 信号灯 Lamp 

颜色设置,Lamp.Color 设置后面参数为RGB数值,例如信号灯显示绿色灯代码如下:

app.Lamp.Color = '0.00,1.00,0.00';

2.1.2 编辑字段 EditField

编辑字段中的文本,指定为字符向量或字符串标量。EditField.Value获取字段值。获取连接名代码如下:

connectName = app.EditField_connectName.Value;

2.1.3 按钮PushButton

        填写按钮相应回调函数即可。

2.1.4  下拉框 DropDown

  获取当前下拉框内容:

value = app.DropDown.Value;

 设置下拉框下拉项:

        下拉项,指定为字符向量元胞数组、字符串数组或一维分类数组。允许重复的元素。下拉组件显示的选项与 Items 数组中的元素数量一样多。比如设置下拉框选项为'Red'、'Yellow'、'Blue'。

app.DropDown.Items = {'Red','Yellow','Blue'}

2.1.5 表 UITable

 设置表名

        设置表名为'Red'、'Yellow'、'Blue'代码如下:

app.UITable.ColumnName = {'Red','Yellow','Blue'};

设置表数据 Data

   表数据,指定为以下类型的数组之一:

  • 表数组

  • 数值数组 - 显示数值,例如 double 或 single

  • 逻辑数组 - 显示复选框。true 值对应于选中复选框,而 false 值显示不选中复选框。

  • 元胞数组 - 显示数值、逻辑值或字符数组值的任意组合。

  • 字符串数组 - 显示字符和文本。

  • 字符向量元胞数组 - 显示字符和文本。

       设置表数据代码如下:

    data = [1,2,3;4,5,6;7,8,9];            
    app.UITable.Data = data;

 完成表名和数据后的表格如下:

2.1.6  容器面板 Panel

        可以放置控件,放置好的UI界面及控件对象名称如下:

2.2 连接数据库

2.2.1 编写“连接数据库”按钮回调函数

如何将Mysql与MATLAB连接

        function Button_ConnectMySQLPushed(app, event)
            try
                connectName = app.EditField_connectName.Value;
                username = app.EditField_user.Value;
                pwd = app.password;
                app.conn= database(connectName,username,pwd);
                app.DropDown_databases.Items = table2cell(fetch(app.conn,'showdatabases'))';
                app.Panel_2.Enable = 'on';
                app.show_ComboboxName()
                app.APPUIFigure.Icon = '对号.png';
                app.Lamp.Color = '0.00,1.00,0.00';
            catch
                errordlg('数据库连接错误!请确保用户名和密码正确','数据库连接错误')
            end

        end

2.2.2 添加私有属性和私有函数show_ComboboxName()

    properties (Access = private)
        conn % Description
        value_database
        password
    end
    methods (Access = private)

        function show_ComboboxName(app)
            app.value_database = app.DropDown_databases.Value;
            sql_usedatabases = sprintf('use %s',app.value_database);
            exec(app.conn,sql_usedatabases);
            app.DropDown_tables.Items = table2cell(fetch(app.conn,'show tables'))';
        end
    end

2.3 载入数据库

        function Button_ConnectMySQLPushed(app, event)
            try
                connectName = app.EditField_connectName.Value;
                username = app.EditField_user.Value;
                pwd = app.password;
                app.conn= database(connectName,username,pwd);
                app.DropDown_databases.Items = able2cell(fetch(app.conn,'showdatabases'))';
                app.Panel_2.Enable = 'on';
                app.show_ComboboxName()
                app.APPUIFigure.Icon = '对号.png';
                app.Lamp.Color = '0.00,1.00,0.00';
            catch
                errordlg('数据库连接错误!请确保用户名和密码正确','数据库连接错误')
            end

        end

3、整体代码

classdef app1 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        APPUIFigure            matlab.ui.Figure
        Label_9                matlab.ui.control.Label
        Lamp                   matlab.ui.control.Lamp
        Panel_2                matlab.ui.container.Panel
        Button_LoadData        matlab.ui.control.Button
        DropDown_tables        matlab.ui.control.DropDown
        Label_7                matlab.ui.control.Label
        DropDown_databases     matlab.ui.control.DropDown
        Label_6                matlab.ui.control.Label
        Panel                  matlab.ui.container.Panel
        EditField_connectName  matlab.ui.control.EditField
        Label_8                matlab.ui.control.Label
        Button_ConnectMySQL    matlab.ui.control.Button
        EditField_pwd          matlab.ui.control.EditField
        Label_5                matlab.ui.control.Label
        EditField_user         matlab.ui.control.EditField
        Label_4                matlab.ui.control.Label
        Label                  matlab.ui.control.Label
        UITable                matlab.ui.control.Table
    end


    properties (Access = private)                                                              
        conn % Description                                                                          
        value_database
        password
    end

    methods (Access = private)

        function results = show_ComboboxName(app)
            app.value_database = app.DropDown_databases.Value;
            sql_usedatabases = sprintf('use %s',app.value_database);
            exec(app.conn,sql_usedatabases);
            app.DropDown_tables.Items = table2cell(fetch(app.conn,'show tables'))';
        end
    end


    % Callbacks that handle component events
    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app)
            app.Panel_2.Enable = 'off';
        end

        % Button pushed function: Button_LoadData
        function Button_LoadDataPushed(app, event)
            try
                sql_usedatabases = sprintf('use %s',app.value_database);
                exec(app.conn,sql_usedatabases);
                value_tables = app.DropDown_tables.Value;
                sql_selectTables = sprintf('select * from %s',value_tables);
                data=fetch(app.conn,sql_selectTables);%执行sql语句,获取users表格数据
                app.UITable.ColumnName=data.Properties.VariableNames;
                app.UITable.Data = data;
            catch
                errordlg('此表格不支持查询,请换表查询','查询失败')
            end
        end

        % Button pushed function: Button_ConnectMySQL
        function Button_ConnectMySQLPushed(app, event)
            try
                connectName = app.EditField_connectName.Value;
                username = app.EditField_user.Value;
                pwd = app.password;
                app.conn= database(connectName,username,pwd);
                app.DropDown_databases.Items = table2cell(fetch(app.conn,'show databases'))';
                app.Panel_2.Enable = 'on';
                app.show_ComboboxName()
                app.APPUIFigure.Icon = '对号.png';
                app.Lamp.Color = '0.00,1.00,0.00';
            catch
                errordlg('数据库连接错误!请确保用户名和密码正确','数据库连接错误')
            end

        end

        % Value changed function: DropDown_databases
        function DropDown_databasesValueChanged(app, event)
            app.show_ComboboxName()
        end
    end

    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create APPUIFigure and hide until all components are created
            app.APPUIFigure = uifigure('Visible', 'off');
            app.APPUIFigure.Color = [0.902 0.902 0.902];
            app.APPUIFigure.Position = [100 100 562 543];
            app.APPUIFigure.Name = '数据库连接APP ';
            app.APPUIFigure.Icon = '叉号.png';

            % Create UITable
            app.UITable = uitable(app.APPUIFigure);
            app.UITable.ColumnName = '';
            app.UITable.RowName = {};
            app.UITable.Position = [32 213 499 277];

            % Create Label
            app.Label = uilabel(app.APPUIFigure);
            app.Label.Position = [32 489 65 25];
            app.Label.Text = '显示数据表';

            % Create Panel
            app.Panel = uipanel(app.APPUIFigure);
            app.Panel.Title = '数据库登录';
            app.Panel.Position = [32 10 224 188];

            % Create Label_4
            app.Label_4 = uilabel(app.Panel);
            app.Label_4.HorizontalAlignment = 'right';
            app.Label_4.Position = [14 91 53 22];
            app.Label_4.Text = '用户名:';

            % Create EditField_user
            app.EditField_user = uieditfield(app.Panel, 'text');
            app.EditField_user.Tooltip = {'数据库登录名称'};
            app.EditField_user.Position = [82 91 100 22];

            % Create Label_5
            app.Label_5 = uilabel(app.Panel);
            app.Label_5.HorizontalAlignment = 'right';
            app.Label_5.Position = [15 53 52 22];
            app.Label_5.Text = '密   码:';

            % Create EditField_pwd
            app.EditField_pwd = uieditfield(app.Panel, 'text');
            app.EditField_pwd.Tooltip = {'数据库登录密码'};
            app.EditField_pwd.Position = [82 53 100 22];

            % Create Button_ConnectMySQL
            app.Button_ConnectMySQL = uibutton(app.Panel, 'push');
            app.Button_ConnectMySQL.ButtonPushedFcn = createCallbackFcn(app, @Button_ConnectMySQLPushed, true);
            app.Button_ConnectMySQL.Icon = '断开连接.png';
            app.Button_ConnectMySQL.Position = [82 11 100 24];
            app.Button_ConnectMySQL.Text = '连接数据库';

            % Create Label_8
            app.Label_8 = uilabel(app.Panel);
            app.Label_8.HorizontalAlignment = 'right';
            app.Label_8.Position = [14 130 53 22];
            app.Label_8.Text = '数据源:';

            % Create EditField_connectName
            app.EditField_connectName = uieditfield(app.Panel, 'text');
            app.EditField_connectName.Tooltip = {'ODCB数据源名称'};
            app.EditField_connectName.Position = [82 130 100 22];

            % Create Panel_2
            app.Panel_2 = uipanel(app.APPUIFigure);
            app.Panel_2.Title = '数据选择';
            app.Panel_2.Position = [307 10 224 188];

            % Create Label_6
            app.Label_6 = uilabel(app.Panel_2);
            app.Label_6.HorizontalAlignment = 'right';
            app.Label_6.Position = [10 130 77 22];
            app.Label_6.Text = '选择数据库:';

            % Create DropDown_databases
            app.DropDown_databases = uidropdown(app.Panel_2);
            app.DropDown_databases.Items = {};
            app.DropDown_databases.ValueChangedFcn = createCallbackFcn(app, @DropDown_databasesValueChanged, true);
            app.DropDown_databases.Position = [102 130 100 22];
            app.DropDown_databases.Value = {};

            % Create Label_7
            app.Label_7 = uilabel(app.Panel_2);
            app.Label_7.HorizontalAlignment = 'right';
            app.Label_7.Position = [10 84 77 22];
            app.Label_7.Text = '选择数据表:';

            % Create DropDown_tables
            app.DropDown_tables = uidropdown(app.Panel_2);
            app.DropDown_tables.Items = {};
            app.DropDown_tables.Position = [102 84 100 22];
            app.DropDown_tables.Value = {};

            % Create Button_LoadData
            app.Button_LoadData = uibutton(app.Panel_2, 'push');
            app.Button_LoadData.ButtonPushedFcn = createCallbackFcn(app, @Button_LoadDataPushed, true);
            app.Button_LoadData.Icon = '载入.png';
            app.Button_LoadData.Position = [114 11 100 24];
            app.Button_LoadData.Text = '载入数据';

            % Create Lamp
            app.Lamp = uilamp(app.APPUIFigure);
            app.Lamp.Position = [501 506 20 20];
            app.Lamp.Color = [0.8 0.8 0.8];

            % Create Label_9
            app.Label_9 = uilabel(app.APPUIFigure);
            app.Label_9.Position = [409 505 89 22];
            app.Label_9.Text = '数据库连接状态';

            % Show the figure after all components are created
            app.APPUIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = app1

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.APPUIFigure)

            % Execute the startup function
            runStartupFcn(app, @startupFcn)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.APPUIFigure)
        end
    end
end
  • 5
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
MATLABMYSQL爬虫-zhgd.m 本帖最后由 caicaibi 于 2018-7-20 11:48 编辑 一、引言         最近有朋友问我MATLAB怎么弄爬虫,下载的大量数据怎么实现归类,借此,我弄了一个MATLAB爬虫,然后将下载的数据存储到MYSQL数据库的例子,在这里分享给大家。希望想做这方面的朋友,能对基本内容有所了解。 二、环境配置 1.MATLAB R2012a   2.安装JDBC驱动 链接:https://pan.baidu.com/s/18GarT6io5LgQy1nfoRL-5g 密码:alpp 3.MYSQL(很多新手看到MYSQL的cmd框,头都要大了,不知道怎么开启,关闭等操作,建议新人不要直接安装MYSQL,去下载wampserver2.5,它里面包含了MYSQL,是一个集成化的包,安装非常方便,在运行时,只要看到它的标志变绿,就可以了。) 软件图标:                                 正常运行状况:                                 4.Navicat for mysql 链接:https://pan.baidu.com/s/1904BVG-OOXlnn2W5DdlIkA 密码:3xr0 软件图标:                                 软件界面: 见后面图片 三、爬虫抓取界面解析 1.抓取对象:          航光电 -> 产品心 2.抓取网址: http://www.jonhon.cn/procuct/show-388.aspx 3.抓取内容: 见后面图片 4.对象特征: i.类别:   见后面图片 分析:每个对象都是一个  href标签,里面有  /cplist1.aspx?category_id=2 类似的标志,作为正则表达式的筛选对象 ii.图片:见后面图片 分析:每张图片都为   bimg 图片,作为正则表达式的筛选对象。注意:每种产品不一定是一张图片,需要对多张图片进行考虑 iii.简介:见后面图片 分析: 简介末尾都有    <!--/商品属性-->   ,作为正则表达式的筛选对象 四、MYSQL操作 1.开启mysql 运行wampserver.exe,绿色图标表示成功运行。 2.创建数据库数据表 具体见教程: http://www.formysql.com/jiqiao/mysql-xinjianbiao.html 3.MATLAB连接MYSQL conn = database;%连接数据库 cursorA = exec;%执行增删改查操作 cur = fetch;%返回结果,最后得到的数据以cell格式,存在cur.Data复制代码 参数解析: database; 第一个参数:数据库的名称,就是要操作的数据库的名称 第二个参数:用户名 第三个参数:密码 第四个参数:连接的驱动,这里就写这个,不用改 第五个参数:数据库的连接路径吧,jdbc:mysql://,前面这个是jdbc,用mysql数据库,后边是具体的路径,数据库的IP,端口,和数据库的名称,跟第一个参数一样 五、程序流程 1.开启数据库 -> 使用循环,凑齐网址 -> 采用urlread抓取页面 2.根据抓取的对象html特征,制定不同的正则表达式筛选出需要的内容     2.1类别:提前的关键字,用\连接,组成创建目录的格式,mkdir函数创建目录     2.2图片:提取图片地址,判断图片数量,去前缀,补充链接,使用urlwrite 将图片下载到目录,保存。     2.3简介:提取标签,新建readme.txt,将简介写入文本 3.文本写入数据库     将[编号,目录,图片数量,网址,摘要]写入数据库。 具体命令,学习sql语句 sql学习网址:http://www.w3school.com.cn/sql/index.asp 六、程序代码 见附件 七、结果 见后面图片 见后面图片
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值