1、程序启动后的界面
2、点击添加文件夹按钮,可新建文件夹
3、程序启动后,如果当前有文件夹和文件,可读取在列表中,对“题目”文件夹中的文件,可删除
4、对“答案”文件夹中的文件,可修改
5、可添加文件夹,并添加文件
代码:
classdef StoreFile < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Label_3 matlab.ui.control.Label
Label_2 matlab.ui.control.Label
Label_1 matlab.ui.control.Label
m_Folderlist matlab.ui.control.ListBox
m_Quslist matlab.ui.control.ListBox
m_Anslist matlab.ui.control.ListBox
m_Addbtn_1 matlab.ui.control.Button
m_Addbtn_2 matlab.ui.control.Button
m_Surebtn matlab.ui.control.Button
m_Deletebtn matlab.ui.control.Button
m_Image matlab.ui.control.UIAxes
end
properties (Access = private)
RootFolder = {} %根文件夹
CurrentFolder_names %所有子文件夹(仅名字)
CurrentQus_names %某子文件夹内所有题目(仅名字)
CurrentAns_names %某子文件夹内所有答案(仅名字)
CurrentFolder %选择的文件夹(仅名字)
CurrentQus %选择的题目(仅名字)
CurrentAns %选择的答案(仅名字)
Qus = '题目'
Ans = '答案'
end
methods (Access = private)
function m_RootFolder_Name(app)
appPath = mfilename('fullpath');
[appDir, ~, ~] = fileparts(appPath);
app.RootFolder = fullfile(appDir,'Fileses');
if ~exist(app.RootFolder,'dir')
mkdir(app.RootFolder);
end
end
function m_changeFolderList(app)%%%读取所有文件夹
folders = dir(app.RootFolder);
folders(strcmp({folders(:).name},'.') | strcmp({folders(:).name},'..')) = [];
app.CurrentFolder_names = {folders.name};
%显示子文件夹
app.m_Folderlist.Items = app.CurrentFolder_names;
end
function m_changeQusList(app)%%%读取所有题目
Ques = dir(fullfile(app.RootFolder, app.CurrentFolder, app.Qus,'*.png'));
app.CurrentQus_names = {Ques.name};
%显示文件
app.m_Quslist.Items = erase(app.CurrentQus_names,".png");
end
function m_changeAnsList(app)%%%读取所有答案
Anses = dir(fullfile(app.RootFolder, app.CurrentFolder, app.Ans,'*.png'));
app.CurrentAns_names = {Anses.name};
%显示文件
app.m_Anslist.Items = erase(app.CurrentAns_names,".png");
end
function m_SureFolders(app)%%%确保"题目"和"答案"文件夹存在
app.m_changeFolderList();
for i=1:length(app.CurrentFolder_names)
m_newFolders_1 = fullfile(app.RootFolder, app.CurrentFolder_names(i), app.Qus);
m_newFolders_2 = fullfile(app.RootFolder, app.CurrentFolder_names(i), app.Ans);
if ~exist(m_newFolders_1{1},'dir')
mkdir(m_newFolders_1{1});
end
if ~exist(m_newFolders_2{1},'dir')
mkdir(m_newFolders_2{1});
end
end
end
function m_newAnswer(app, picture, path)%%%新建答案图片
% 在 UIAxes上添加一个可调整大小和位置的矩形,用于选择要裁剪的区域
app.m_Surebtn.Visible ="on";
rec = drawrectangle(app.m_Image);
rec.Position = [0, 0, size(picture, 2), size(picture, 1)];%默认初始为图片大小
% 等待用户选择区域并按下 Enter 键
app.m_Surebtn.Enable ="on";
wait(rec);
% 使用 imcrop 函数将所选区域裁剪出来,并将其保存到新的文件中
new_Ans = imcrop(picture, rec.Position);
imwrite(new_Ans, path);
delete(rec);
app.m_Surebtn.Visible ="off";
app.m_Surebtn.Enable ="off";<