cla 指定 界面 matlab,用户界面-由Matlabs指南构建的GUI的回调共享的变量

本文介绍了一个使用Matlab创建GUI的工作示例,主要展示了如何通过'handles'方法和全局变量实现90%的GUI交互,包括图片浏览、加载、显示和清除功能。特别强调了在不同场景下使用全局变量的原因,并提供了关键函数如`Browse_Callback`和`load_global_Callback`的实现细节。
摘要由CSDN通过智能技术生成

这是一个工作示例(使用GUIDE),它以两种不同的方式完成您要查找的内容.总的来说,我更喜欢对90%的GUI使用’handles’方法.我唯一会使用全局变量的情况是是否需要在GUI外部访问数据.

请注意,我在打开函数中添加了“ handles.img = 0”.作为免责声明,浏览器中没有数据验证.我也仅使用.gif文件进行了此测试,并且没有考虑将图像显示到轴的最佳方法.只是又快又脏:)

编辑:当您将这些数据复制并粘贴到M文件中时.确保将其命名为picture_loader.m. Matlab用错误的文件名对我做了一些愚蠢的事情:)

希望这可以帮助.

function varargout = picture_loader(varargin)

% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;

gui_State = struct('gui_Name', mfilename, ...

'gui_Singleton', gui_Singleton, ...

'gui_OpeningFcn', @picture_loader_OpeningFcn, ...

'gui_OutputFcn', @picture_loader_OutputFcn, ...

'gui_LayoutFcn', [] , ...

'gui_Callback', []);

if nargin && ischar(varargin{1})

gui_State.gui_Callback = str2func(varargin{1});

end

if 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 picture_loader is made visible.

function picture_loader_OpeningFcn(hObject, eventdata, handles, varargin)

handles.output = hObject;

handles.img = 0; % Add the img data to the handle

guidata(hObject, handles);

% --- Outputs from this function are returned to the command line.

function varargout = picture_loader_OutputFcn(hObject, eventdata, handles)

varargout{1} = handles.output;

% --- Executes on button press in Browse.

function Browse_Callback(hObject, eventdata, handles)

global img % Store the data global

img_path = uigetfile('*.gif'); % browse for a file

img = importdata(img_path); % Load the image data

handles.img = img; % Store the img data in the handles struct

guidata(hObject, handles); % Save handles so all call backs have the updated data

% Plot the data in the axes1

axes(handles.axes1) % Select axes1 to write to

image(img.cdata) % Display the image

colormap(img.colormap) % Apply proper colormap

% --- Executes on button press in load_global.

function load_global_Callback(hObject, eventdata, handles)

global img

if isstruct(img)

axes(handles.axes1) %Select the axes1 on the gui

image(img.cdata)

colormap(img.colormap)

end

% --- Executes on button press in load_global_handle.

function load_handle_Callback(hObject, eventdata, handles)

if isstruct(handles.img)

axes(handles.axes1) %Select the axes1 on the gui

image(handles.img.cdata)

colormap(handles.img.colormap)

end

% --- Executes on button press in clear.

function clear_Callback(hObject, eventdata, handles)

cla(handles.axes1)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值