[编者注:]学生成绩管理并不是matlab的强项,或者说没有发挥matlab的特长,但很多网友还是表现出极大兴趣,所以今天选择一个App Designer平台相关功能的大作业。大作业关键是如何载入学生成绩文件及如何读取任意一门课程成绩。在matlab中采用菜单或按钮工具,使用uigetfile函数,可直接选择用户数据文件,然后用xlsread或readtable函数读取数据内容,如data = readtable('test.xlsx'),或data =xlsread('test.xlsx'),此时如果界面上有表对象,用app.UITable.Data = data代码,就能直接把数据显示在表中,所以,很容易实现表对象操作。基于今天的大作业,主要想展示一下如何在启动应用程序后,界面上表格就显示了数据内容,重点演示startupFcn函数(根据deepSeek-R1的回答方案),这个技巧可用于其他类似的情况,不一定是成绩管理。至于程序的统计功能,今天只演示排序和分级统计两个,其他只能是举一反三了。
一、心得体会
在这段时间里,我像是踏入了一座神秘的数学仙境,那里有着编码的奇迹和矩阵的魔法,而这一切的引导者正是MATLAB。学习MATLAB的过程如同一场深邃的思维冒险,从最初的迷茫到逐渐领略到其强大之处,这段旅程让我不禁沉醉其中。
一开始,我感觉自己仿佛置身于一座陌生的城市,看似复杂而又充满秩序。MATLAB的语法如同城市的街道,初次接触时我总是被这些看似晦涩的代码所迷惑,但渐渐地,我学会了这座城市的语言,开始能够自如地在代码的街巷中穿梭。
就像解谜一样,逐渐解开MATLAB的奥秘是一种愉悦。当我第一次成功地运行一个复杂的算法时,仿佛看到了一扇扇大门敞开,通向了数学的更深层次。那一刻的成就感,就像找到了城市中的宝藏一样,让我对编程充满了热情。
而矩阵运算,则是这座城市的高楼大厦,层层叠叠,密不透风。我逐渐理解了线性代数的美妙之处,从简单的矩阵相乘到复杂的特征值分解,每一步都如同攀登一座高峰,俯瞰着自己所征服的领域。
MATLAB的画图功能更是如一场绚丽的焰火,将抽象的数学概念以直观的方式呈现在我的眼前。曲线、图表和三维空间的展示,仿佛在给予数字生命,让我能够感受到数学的美丽。
总的来说,学习MATLAB是一次探索未知世界的旅程,是一次挑战自我的过程。这座城市充满了数学的魅力,而我也在这片领域中找到了属于自己的舞台。MATLAB,是我的数学奇迹之旅的引路人,开启了我对编程和数学的新认知,让我在计算的世界中找到了乐趣。
二、程序运行情况
1、启动程序后,在“文件名”处输入当前目录下的一下excel文件名(当然可以带路径),然后点击“导入”按钮,成绩出现在表中(这也是一种加载数据的方法)。此时排名列为序号。根据deepSeek-R1的回答方案,表中第一列初始应为序号名称,排序后自动变为排序,可见deepSeek更友好,所以要多读deepSeek的程序。
2、点击“排序”按钮,表中的数据是按分数列降序排列,序号变成排名。根据deepSeek-R1的回答方案,排序后表中第一列序号名称排序后自动变为排序,但排序号是原来的序号,这一点估计deepSeek没理解用户的意思,但我也没追问。
3、点击“绘图”(分级统计),出现弹出窗口,显示直方图。
4、数据文件内容,没有表头。后面为方便deepSeek回答问题,编者加了表头,为适应编程环境,表头满足变量命名规则。
三、大作业设计视图
四、大作业代码视图
classdef project < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Button_3 matlab.ui.control.StateButton
EditField matlab.ui.control.EditField
Label matlab.ui.control.Label
Button_2 matlab.ui.control.StateButton
Button matlab.ui.control.Button
UITable matlab.ui.control.Table
end
properties (Access = public)
info % Description
RowNumber % Description
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: Button_2
function Button_2ValueChanged(app, event)
adress=app.EditField.Value;
app.info=xlsread(adress);
app.RowNumber = size(app.info,1);
app.UITable.Data = app.info;
end
% Button pushed function: Button
function ButtonPushed(app, event)
for i=1:app.RowNumber
for j=i:app.RowNumber
if app.info(i,3)<app.info(j,3)
tempnum=app.info(i,2);
tempscore=app.info(i,3);
app.info(i,2)=app.info(j,2);
app.info(i,3)=app.info(j,3);
app.info(j,2)=tempnum;
app.info(j,3)=tempscore;
end
end
end
app.UITable.Data = app.info;
end
% Value changed function: Button_3
function Button_3ValueChanged(app, event)
num=[0,0,0,0,0,0,0,0,0,0];
for i=1:app.RowNumber
switch app.info(i,3)
case num2cell(0:9)
num(1,1)=num(1,1)+1;
case num2cell(10:19)
num(1,2)=num(1,2)+1;
case num2cell(20:29)
num(1,3)=num(1,3)+1;
case num2cell(30:39)
num(1,4)=num(1,4)+1;
case num2cell(40:49)
num(1,5)=num(1,5)+1;
case num2cell(50:59)
num(1,6)=num(1,6)+1;
case num2cell(60:69)
num(1,7)=num(1,7)+1;
case num2cell(70:79)
num(1,8)=num(1,8)+1;
case num2cell(80:89)
num(1,9)=num(1,9)+1;
case num2cell(90:100)
num(1,10)=num(1,10)+1;
otherwise
end
end
x=[5,15,25,35,45,55,65,75,85,95];
bar(x,num,1);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create UITable
app.UITable = uitable(app.UIFigure);
app.UITable.ColumnName = {'排名'; '学号'; '分数'};
app.UITable.RowName = {};
app.UITable.ColumnSortable = [false true true];
app.UITable.Position = [22 245 583 209];
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
app.Button.Position = [33 25 72 34];
app.Button.Text = '排序';
% Create Button_2
app.Button_2 = uibutton(app.UIFigure, 'state');
app.Button_2.ValueChangedFcn = createCallbackFcn(app, @Button_2ValueChanged, true);
app.Button_2.Text = '导入';
app.Button_2.Position = [534 179 71 34];
% Create Label
app.Label = uilabel(app.UIFigure);
app.Label.HorizontalAlignment = 'right';
app.Label.Position = [33 187 41 22];
app.Label.Text = '文件名';
% Create EditField
app.EditField = uieditfield(app.UIFigure, 'text');
app.EditField.Position = [89 179 438 38];
% Create Button_3
app.Button_3 = uibutton(app.UIFigure, 'state');
app.Button_3.ValueChangedFcn = createCallbackFcn(app, @Button_3ValueChanged, true);
app.Button_3.Text = '绘图';
app.Button_3.Position = [128 25 72 33];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = project
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
五、代码形式程序
.rtcContent { padding: 30px; } .lineNode {font-size: 10pt; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-style: normal; font-weight: normal; }classdef project < matlab.apps.AppBase % Properties that correspond to app components properties (Access = public) UIFigure matlab.ui.Figure Button_3 matlab.ui.control.StateButton EditField matlab.ui.control.EditField Label matlab.ui.control.Label Button_2 matlab.ui.control.StateButton Button matlab.ui.control.Button UITable matlab.ui.control.Table end properties (Access = public) info % Description RowNumber % Description end % Callbacks that handle component events methods (Access = private) % Value changed function: Button_2 function Button_2ValueChanged(app, event) adress=app.EditField.Value; app.info=xlsread(adress); app.RowNumber = size(app.info,1); app.UITable.Data = app.info; end % Button pushed function: Button function ButtonPushed(app, event) for i=1:app.RowNumber for j=i:app.RowNumber if app.info(i,3)<app.info(j,3) tempnum=app.info(i,2); tempscore=app.info(i,3); app.info(i,2)=app.info(j,2); app.info(i,3)=app.info(j,3); app.info(j,2)=tempnum; app.info(j,3)=tempscore; end end end app.UITable.Data = app.info; end % Value changed function: Button_3 function Button_3ValueChanged(app, event) num=[0,0,0,0,0,0,0,0,0,0]; for i=1:app.RowNumber switch app.info(i,3) case num2cell(0:9) num(1,1)=num(1,1)+1; case num2cell(10:19) num(1,2)=num(1,2)+1; case num2cell(20:29) num(1,3)=num(1,3)+1; case num2cell(30:39) num(1,4)=num(1,4)+1; case num2cell(40:49) num(1,5)=num(1,5)+1; case num2cell(50:59) num(1,6)=num(1,6)+1; case num2cell(60:69) num(1,7)=num(1,7)+1; case num2cell(70:79) num(1,8)=num(1,8)+1; case num2cell(80:89) num(1,9)=num(1,9)+1; case num2cell(90:100) num(1,10)=num(1,10)+1; otherwise end end x=[5,15,25,35,45,55,65,75,85,95]; bar(x,num,1); end end % Component initialization methods (Access = private) % Create UIFigure and components function createComponents(app) % Create UIFigure and hide until all components are created app.UIFigure = uifigure('Visible', 'off'); app.UIFigure.Position = [100 100 640 480]; app.UIFigure.Name = 'MATLAB App'; % Create UITable app.UITable = uitable(app.UIFigure); app.UITable.ColumnName = {'排名'; '学号'; '分数'}; app.UITable.RowName = {}; app.UITable.ColumnSortable = [false true true]; app.UITable.Position = [22 245 583 209]; % Create Button app.Button = uibutton(app.UIFigure, 'push'); app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true); app.Button.Position = [33 25 72 34]; app.Button.Text = '排序'; % Create Button_2 app.Button_2 = uibutton(app.UIFigure, 'state'); app.Button_2.ValueChangedFcn = createCallbackFcn(app, @Button_2ValueChanged, true); app.Button_2.Text = '导入'; app.Button_2.Position = [534 179 71 34]; % Create Label app.Label = uilabel(app.UIFigure); app.Label.HorizontalAlignment = 'right'; app.Label.Position = [33 187 41 22]; app.Label.Text = '文件名'; % Create EditField app.EditField = uieditfield(app.UIFigure, 'text'); app.EditField.Position = [89 179 438 38]; % Create Button_3 app.Button_3 = uibutton(app.UIFigure, 'state'); app.Button_3.ValueChangedFcn = createCallbackFcn(app, @Button_3ValueChanged, true); app.Button_3.Text = '绘图'; app.Button_3.Position = [128 25 72 33]; % Show the figure after all components are created app.UIFigure.Visible = 'on'; end end % App creation and deletion methods (Access = public) % Construct app function app = project % Create UIFigure and components createComponents(app) % Register the app with App Designer registerApp(app, app.UIFigure) if nargout == 0 clear app end end % Code that executes before app deletion function delete(app) % Delete UIFigure when app is deleted delete(app.UIFigure) end endend
六、大作业文件目录清单
七、deepSeek-R1的实现
1、提问
请帮我在matlab环境下用App Designer编写一个统计成绩的应用程序,有界面,打开一个excel文件,成绩显示在界面上的表中,表头分别为排序、学号、分数,与excel文件数据对应,分别是序号、学号、成绩,见test.xlsx。界面上需要创建排序和统计两个按钮,排序结果就显示在表中,统计直方图另外的窗口显示,分10段,每段是10个整数。
2、思考
3、回答
4、实现
(1)deepSeek-R1步骤 1:创建App界面。
在MATLAB中新建一个App Designer项目。
(2)添加以下组件:
a.表格控件 (UITable):用于显示数据。
b.排序按钮 (Button):用于触发排序功能。
c.统计按钮 (Button):用于生成直方图。
(3)deepSeek-R1步骤 2:设置回调函数
在“排序”按钮上右击,在现场弹出菜单上选择添加回调,matlab即转至代码视图,让用户添加程序内容。注意:App Designer自动生成的代码视图,只是框架,与GUIDE性质相同,但在GUIDE中可以修改任何一行语句,但在App Designer中只能添加用户程序,应用程序结构内容不可以修改,以保证程序不会出错,以属于安全机制,所以,添加回调函数内容必须通过添加callback方式来添加,并自动关联,或通过编辑器添加函数功能,但需要手动与控件关联,没有关联的函数不会执行。
把deepSeek-R1给的代码粘贴在可修改处(白底)
通过编辑器的添加函数功能,增加回调函数
(4)deepSeek-R1步骤 3:设置组件属性
点击设计视图右侧“组件浏览器”中的程序名称,再点击“回调”,选择“startupFcn”的回调函数为startupFcn,即完成关联。这样程序启动后,即完成数据文件读取,并显示在表中。
排序按钮和统计按钮的回调属性已自动关联
(5)程序运行结果
a.启动程序,第一列名称为“序号”
b.排序后,第一列名称变为排序,但内容是原来的序号
c.统计,显示分级统计直方图
d.增加表头,为减少程序代码,直接增加表头内容
5、程序代码视图
classdef chengjiguanli < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
SortButton matlab.ui.control.Button
StatsButton matlab.ui.control.Button
UITable matlab.ui.control.Table
end
properties (Access = private)
Data table % 存储Excel数据
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
try
% 读取Excel文件
filename = 'test.xlsx';
opts = detectImportOptions(filename);
opts.VariableTypes = {'double', 'char', 'double'}; % 指定列类型
data = readtable(filename, opts);
% 处理学号列格式
data.xuehao = strrep(data.xuehao, ' ', ''); % 去除空格
app.Data = data;
% 更新表格显示
app.UITable.Data = table2cell(data);
end
end
% Button pushed function: SortButton
function SortButtonPushed(app, event)
% 按分数降序排序
sortedData = sortrows(app.Data, 'fenshu', 'descend');
app.UITable.ColumnName = {'排序', '学号', '分数'};
app.UITable.Data = table2cell(sortedData);
end
% Button pushed function: StatsButton
function StatsButtonPushed(app, event)
% 创建新窗口显示直方图
% 提取分数数据
scores = app.Data.fenshu;
% 创建新窗口
fig = figure('Name', '成绩分布直方图');
ax = axes(fig);
% 绘制直方图
edges = 0:10:100;
histogram(ax, scores, edges, 'FaceColor', 'blue');
title(ax, '成绩分布');
xlabel(ax, '分数段');
ylabel(ax, '人数');
xticks(ax, edges);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create UITable
app.UITable = uitable(app.UIFigure);
app.UITable.ColumnName = {'序号'; '学号'; '分数'};
app.UITable.RowName = {};
app.UITable.Tag = 'UITable';
app.UITable.Position = [137 21 491 437];
% Create StatsButton
app.StatsButton = uibutton(app.UIFigure, 'push');
app.StatsButton.ButtonPushedFcn = createCallbackFcn(app, @StatsButtonPushed, true);
app.StatsButton.Tag = 'StatsButton';
app.StatsButton.Position = [11 147 108 94];
app.StatsButton.Text = '统计';
% Create SortButton
app.SortButton = uibutton(app.UIFigure, 'push');
app.SortButton.ButtonPushedFcn = createCallbackFcn(app, @SortButtonPushed, true);
app.SortButton.Tag = 'SortButton';
app.SortButton.Position = [11 321 108 94];
app.SortButton.Text = '排序';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = chengjiguanli
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% 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.UIFigure)
end
end
end
6、代码形式代码视图
.rtcContent { padding: 30px; } .lineNode {font-size: 24pt; font-family: "宋体", Menlo, Monaco, Consolas, "Courier New", monospace; font-style: normal; font-weight: normal; }classdef chengjiguanli < matlab.apps.AppBase % Properties that correspond to app components properties (Access = public) UIFigure matlab.ui.Figure SortButton matlab.ui.control.Button StatsButton matlab.ui.control.Button UITable matlab.ui.control.Table end properties (Access = private) Data table % 存储Excel数据 end % Callbacks that handle component events methods (Access = private) % Code that executes after component creation function startupFcn(app) try % 读取Excel文件 filename = 'test.xlsx'; opts = detectImportOptions(filename); opts.VariableTypes = {'double', 'char', 'double'}; % 指定列类型 data = readtable(filename, opts); % 处理学号列格式 data.xuehao = strrep(data.xuehao, ' ', ''); % 去除空格 app.Data = data; % 更新表格显示 app.UITable.Data = table2cell(data); end end % Button pushed function: SortButton function SortButtonPushed(app, event) % 按分数降序排序 sortedData = sortrows(app.Data, 'fenshu', 'descend'); app.UITable.ColumnName = {'排序', '学号', '分数'}; app.UITable.Data = table2cell(sortedData); end % Button pushed function: StatsButton function StatsButtonPushed(app, event) % 创建新窗口显示直方图 % 提取分数数据 scores = app.Data.fenshu; % 创建新窗口 fig = figure('Name', '成绩分布直方图'); ax = axes(fig); % 绘制直方图 edges = 0:10:100; histogram(ax, scores, edges, 'FaceColor', 'blue'); title(ax, '成绩分布'); xlabel(ax, '分数段'); ylabel(ax, '人数'); xticks(ax, edges); end end % Component initialization methods (Access = private) % Create UIFigure and components function createComponents(app) % Create UIFigure and hide until all components are created app.UIFigure = uifigure('Visible', 'off'); app.UIFigure.Position = [100 100 640 480]; app.UIFigure.Name = 'MATLAB App'; % Create UITable app.UITable = uitable(app.UIFigure); app.UITable.ColumnName = {'序号'; '学号'; '分数'}; app.UITable.RowName = {}; app.UITable.Tag = 'UITable'; app.UITable.Position = [137 21 491 437]; % Create StatsButton app.StatsButton = uibutton(app.UIFigure, 'push'); app.StatsButton.ButtonPushedFcn = createCallbackFcn(app, @StatsButtonPushed, true); app.StatsButton.Tag = 'StatsButton'; app.StatsButton.Position = [11 147 108 94]; app.StatsButton.Text = '统计'; % Create SortButton app.SortButton = uibutton(app.UIFigure, 'push'); app.SortButton.ButtonPushedFcn = createCallbackFcn(app, @SortButtonPushed, true); app.SortButton.Tag = 'SortButton'; app.SortButton.Position = [11 321 108 94]; app.SortButton.Text = '排序'; % Show the figure after all components are created app.UIFigure.Visible = 'on'; end end % App creation and deletion methods (Access = public) % Construct app function app = chengjiguanli % Create UIFigure and components createComponents(app) % Register the app with App Designer registerApp(app, app.UIFigure) % 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.UIFigure) end endend