Matlab GUI设计——文件读取和保存uigetfile,uiputfile
来源:互联网 发布:js验证用户名是否存在 编辑:程序博客网 时间:2024/06/06 01:56
这一篇主要介绍一些Matlab GUI制作时常用到的文件读取和保存的函数:uigetfile,uiputfile。具体的语法就不再重复,主要通过一些自己编写的GUI来进行介绍,也许这样更有帮助:
1. uigetfile:该函数返回值包含读取的文件的名称cell数组(FileName),文件的路径数组(FilePath),判断读取是否成功的标志FileIndex(不等于0时表示成功)。我们分析下面的matlab代码来进行具体讲解:我们通过在Callback函数中添加uigetfile函数来实现在界面中读入视频,注意到在代码中uigetfile的返回值正好为之前介绍过的三个值,不再介绍,同时{}中表示读取文件的格式选择,‘ReadVideo'则为读取数据的对话框的名称,’MultiSelect'取值为‘on'表示一次可以打开多个视频,当打开的是多个视频时,FileName是一个cell数组,如果取值为’off'时,则只能打开一个视频;还有就是可以设置默认路径,即弹出的对话框中最先能够看到的文件的路径。
注意:uigetfile只是获得了文件的路径和文件名称,并没有其他的任何操作,真正读取数据,需要相应的读取函数,例如图片可以用imread,视频可以用VideoReader对象读取。
function ReadVideoIO_Callback(hObject, eventdata, handles)% hObject handle to ReadVideoIO (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)%读入视频,前三个是第一次申明,第四个是引用global InputVideo;global ResultVideo;global VideoFullPath;global isVideo;[ReadVideoFileName,ReadVideoPathName,ReadVideoFilterIndex] = uigetfile({'*.avi;*.mp4','VideoFile(*.avi,*.mp4)';'*.avi','AVIVideoFile(*.avi)';'*.*','AllFile(*.*)'},'ReadVideo',... 'MultiSelect','on',... %是否能够多选,'off'不支持多选, 'on'支持多选 'C:\Users\hsw\Desktop'); %设置默认路径if isequal(ReadVideoFileName,0) || isequal(ReadVideoPathName,0) || isequal(ReadVideoFilterIndex,0) msgbox('导入视频失败,点击 确定 关闭对话框,再重新导入');else %支持多选时需要处理 isVideo = 1; if iscell(ReadVideoFileName) %读入多个视频时 InputVideo = cell(length(ReadVideoFileName),1); VideoFullPath = InputVideo; for IterVideo = 1:length(ReadVideoFileName) VideoFullPath{IterVideo} = fullfile(ReadVideoPathName,ReadVideoFileName{IterVideo}); %先保存所有视频或图像路径 end VideoObject = VideoReader(VideoFullPath{1}); else %只读入一个视频时 VideoFullPath = fullfile(ReadVideoPathName,ReadVideoFileName); VideoObject = VideoReader(VideoFullPath); end % 显示第一个视频的第一帧,直到按下Start按钮时,开始显示别的 frame = read(VideoObject,1); axes(handles.OriginalAxes); imshow(frame); axes(handles.ResultAxes); imshow(255*ones(size(frame))); ResultVideo = InputVideo; msgbox('成功导入视频,点击 确定 关掉对话框');end
2. uiputfile:该函数的返回值同uigetfile函数相同,但是,没有了‘MultiSelect’属性,即一次只能一次保存一个文件,当然,这里是不会真正保存文件的,而是返回路径+文件名称,从而,需要添加真正保存文件的代码,例如imwrite保存图像
3. 一个简单的界面:
function varargout = TestGUI(varargin)% TESTGUI MATLAB code for TestGUI.fig% TESTGUI, by itself, creates a new TESTGUI or raises the existing% singleton*.%% H = TESTGUI returns the handle to a new TESTGUI or the handle to% the existing singleton*.%% TESTGUI('CALLBACK',hObject,eventData,handles,...) calls the local% function named CALLBACK in TESTGUI.M with the given input arguments.%% TESTGUI('Property','Value',...) creates a new TESTGUI or raises the% existing singleton*. Starting from the left, property value pairs are% applied to the GUI before TestGUI_OpeningFcn gets called. An% unrecognized property name or invalid value makes property application% stop. All inputs are passed to TestGUI_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 TestGUI% Last Modified by GUIDE v2.5 06-Oct-2015 11:48:44% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @TestGUI_OpeningFcn, ... 'gui_OutputFcn', @TestGUI_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []);if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1});endif 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 TestGUI is made visible.function TestGUI_OpeningFcn(hObject, eventdata, handles, varargin)% 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 TestGUI (see VARARGIN)% 定义全局变量global isVideo;isVideo = 0;global isImage;isImage = 0;global Count;Count = 0;% Choose default command line output for TestGUIhandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes TestGUI wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.function varargout = TestGUI_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 structurevarargout{1} = handles.output;% --------------------------------------------------------------------function ImageIO_Callback(hObject, eventdata, handles)% hObject handle to ImageIO (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------function VideoIO_Callback(hObject, eventdata, handles)% hObject handle to VideoIO (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------function ReadVideoIO_Callback(hObject, eventdata, handles)% hObject handle to ReadVideoIO (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)%读入视频,前三个是第一次申明,第四个是引用global InputVideo;global ResultVideo;global VideoFullPath;global isVideo;[ReadVideoFileName,ReadVideoPathName,ReadVideoFilterIndex] = uigetfile({'*.avi;*.mp4','VideoFile(*.avi,*.mp4)';'*.avi','AVIVideoFile(*.avi)';'*.*','AllFile(*.*)'},'ReadVideo',... 'MultiSelect','on',... %是否能够多选,'off'不支持多选, 'on'支持多选 'C:\Users\hsw\Desktop'); %设置默认路径if isequal(ReadVideoFileName,0) || isequal(ReadVideoPathName,0) || isequal(ReadVideoFilterIndex,0) msgbox('导入视频失败,点击 确定 关闭对话框,再重新导入');else %支持多选时需要处理 isVideo = 1; if iscell(ReadVideoFileName) %读入多个视频时 InputVideo = cell(length(ReadVideoFileName),1); VideoFullPath = InputVideo; for IterVideo = 1:length(ReadVideoFileName) VideoFullPath{IterVideo} = fullfile(ReadVideoPathName,ReadVideoFileName{IterVideo}); %先保存所有视频或图像路径 end VideoObject = VideoReader(VideoFullPath{1}); else %只读入一个视频时 VideoFullPath = fullfile(ReadVideoPathName,ReadVideoFileName); VideoObject = VideoReader(VideoFullPath); end % 显示第一个视频的第一帧,直到按下Start按钮时,开始显示别的 frame = read(VideoObject,1); axes(handles.OriginalAxes); imshow(frame); axes(handles.ResultAxes); imshow(255*ones(size(frame))); ResultVideo = InputVideo; msgbox('成功导入视频,点击 确定 关掉对话框');end% --------------------------------------------------------------------function SaveVideoIO_Callback(hObject, eventdata, handles)% hObject handle to SaveVideoIO (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)%保存视频[SaveVideoFileName,SaveVideoPathName,SaveVideoFilterIndex] = uiputfile({'*.avi;*.mp4','VideoFile(*.avi,*.mp4)';... '*.avi','AVIVideoFile(*.avi)';'*.*','AllFile(*.*)'},'ReadVideo',... 'C:\Users\hsw\Desktop'); %设置默认路径if isequal(SaveVideoFileName,0) || isequal(SaveVideoPathName,0) || isequal(SaveVideoFilterIndex,0) disp('User seleceted Cancel');else %这里保存所有读入的视频的处理结果 end% --------------------------------------------------------------------function ReadImageIO_Callback(hObject, eventdata, handles)% hObject handle to ReadImageIO (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)%读入图像,前三个是第一次申明,第四个是引用global InputImage;global ResultImage;global ImageFullPath;global isImage;[ReadImageFileName,ReadImagePathName,ReadImageFilterIndex] = uigetfile({'*.jpg;*.png;*.tif','ImageFile(*.jpg;*.png;*.tif)';... '*.jpg','JPEGImageFile(*.jpg)';'*.*','AllFile(*.*)'},'ReadImage',... 'MultiSelect','on',... %是否能够多选,'off'不支持多选, 'on'支持多选 'C:\Users\hsw\Desktop'); %设置默认路径if isequal(ReadImageFileName,0)|| isequal(ReadImagePathName,0) || isequal(ReadImageFilterIndex,0) msgbox('导入图像失败,点击 确定 关闭对话框,再重新导入');else % 支持多选时,注意需要分别处理 isImage = 1; if iscell(ReadImageFileName) %读入多个图像时,名称为cell数组,多个图像必须在同一个目录 InputImage = cell(length(ReadImageFileName),1); ImageFullPath = InputImage; for IterImage = 1:length(ReadImageFileName) ImageFullPath{IterImage} = fullfile(ReadImagePathName,ReadImageFileName{IterImage}); end FirstImageFullPath = ImageFullPath{1}; else %只读入一个视频时 FirstImageFullPath = fullfile(ReadImagePathName,ReadImageFileName); ImageFullPath = FirstImageFullPath; end %显示第一张图片 axes(handles.OriginalAxes); imshow(imread(FirstImageFullPath)); axes(handles.ResultAxes); imshow(255*ones(size(imread(FirstImageFullPath)))); ResultImage = InputImage; msgbox('成功导入图像,点击 确定 关掉对话框');end% --------------------------------------------------------------------function SaveImageIO_Callback(hObject, eventdata, handles)% hObject handle to SaveImageIO (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)%保存图像:保存当前结果[SaveImageFileName,SaveImagePathName,SaveImageFilterIndex] = uiputfile({'*.jpg;*.png;*.tif','ImageFile(*.jpg;*.png;*.tif)';... '*.jpg','JPEGImageFile(*.jpg)';'*.*','AllFile(*.*)'},'SaveImage','C:\Users\heshiwen\Desktop');if isequal(SaveImageFileName,0) || isequal(SaveImagePathName,0) || isequal(SaveImageFilterIndex,0) disp('User selected Cancel');else %保存处理的结果 end% --- Executes on button press in StartPushButton.function StartPushButton_Callback(hObject, eventdata, handles)% hObject handle to StartPushButton (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)%开始执行功能%需要再次申明全局变量global InputVideo;global ResultVideo;global VideoFullPath;global isVideo;global InputImage;global ResultImage;global ImageFullPath;global isImage;global Count;% handles 不清楚有哪些按钮可以输出handles查看ChooseFunctions1 = get(handles.radiobutton1,'Value'); %执行radiobutton对应功能ChooseFunctions2 = get(handles.radiobutton2,'Value'); %执行radiobutton对应功能if isImage %处理图像 if ChooseFunctions1 %进行图像翻转 if iscell(ImageFullPath) && Count < length(ImageFullPath) %处理多幅图像 InputImage = imread(ImageFullPath{Count + 1}); ResultImage{Count + 1} = 255*ones(size(InputImage)) - double(InputImage); axes(handles.OriginalAxes); imshow(InputImage); axes(handles.ResultAxes); imshow(ResultImage{Count + 1}/255,[]); Count = Count + 1; if Count == length(ImageFullPath) msgbox('图像处理完!'); Count = 0; end elseif ~iscell(ImageFullPath) && Count < 1 InputImage = imread(ImageFullPath); ResultImage = 255*ones(size(InputImage)) - double(InputImage); axes(handles.OriginalAxes); imshow(InputImage); axes(handles.ResultAxes); imshow(ResultImage/255,[]); Count = Count + 1; if Count == 1 msgbox('图像处理完!'); Count = 0; end end elseif ChooseFunctions2 %进行直方图均衡化 msgbox('没有实现!'); else errordlg('程序出错了!'); endelseif isVideo if ChooseFunctions1 if iscell(VideoFullPath) && Count < length(VideoFullPath) VideoObject = VideoReader(VideoFullPath{Count + 1}); for IterVideo = 1:VideoObject.NumberOfFrames InputFrame = read(VideoObject,IterVideo); ResultFrame = 255*ones(size(InputFrame)) - double(InputFrame); axes(handles.OriginalAxes); imshow(InputFrame); axes(handles.ResultAxes); imshow(ResultFrame/255,[]); end Count = Count + 1; %改如何保存呢? elseif ~iscell(ImageFullPath) && Count < 1 Count = Count + 1; end elseif ChooseFunction2 msgbox('没有实现!'); else errordlg('程序出错了!'); endelse errordlg('需要先导入图像/视频');end% --- Executes on button press in ExitPushButton.function ExitPushButton_Callback(hObject, eventdata, handles)% hObject handle to ExitPushButton (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)%退出导航close(gcf);% msgbox('Exit !!');注意:保存部分没有写,类比读取图像的方法添加就行了
0 0
- Matlab GUI设计——文件读取和保存uigetfile,uiputfile
- Matlab GUI的文件打开和保存uigetfile uigetdir
- GUI存储文件uiputfile matlab
- matlab GUI之常用对话框(一)---uigetfile\ uiputfile \ uisetcolor \ uisetfont
- Matlab常用对话框--------文件保存对话框(uiputfile)
- matlab读取文件和保存文件
- matlab 批量读取和保存文件
- Matlab读取和保存TXT文件数据
- MATLAB GUI设计学习—GUI数据管理
- MATLAB读取文件+保存文件
- matlab的文件打开对话框函数uigetfile
- matlab中的uigetfile函数和setappdata函数
- [matlab] 读取数据-保存数据-文件移动和复制
- MATLAB GUI设计——绘图
- 保存和读取文件
- 文件保存和读取
- 文件保存和读取
- 文件保存和读取
- 每日三个笑话-20151006
- UI 动画之CALayer+CAAnimation 之 CABasicAnimation
- Notepad++配合QuickText打造快速高效的文本编辑器
- 深入理解Java:注解(Annotation)--注解处理器
- 【Python】Learn Python the hard way, ex2 注释
- Matlab GUI设计——文件读取和保存uigetfile,uiputfile
- hdu5073 Galaxy(暴力)
- 二叉树遍历
- 使用insert和select选项,可以把数据从一张表复制到另外一张表
- swif通过代码生成子视图
- c apache2模块开发--根据自定义业务逻辑实现文件下载
- uva 1636 概率
- access 删除字段为空的值
- hdu2079选课时间(动态规划&&母函数)