これは、探していることを 2 つの異なる方法で実行する実際の例 (GUIDE を使用) です。全体として、GUI の 90% で「ハンドル」メソッドを使用することを好みます。グローバルを使用するのは、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)