如何在matlab指南中使用单选按钮显示不同的图像?

如何在matlab指南中使用单选按钮显示不同的图像?,matlab,radio-button,matlab-guide,axes,Matlab,Radio Button,Matlab Guide,Axes,这是目前所有的代码。更改按钮组中的选定按钮时会显示正确的图像,但在更改选定按钮之前不会显示图像。我希望第一个图像是FloorPlan{1},正如它在axes1_CreateFcn中所说的那样,但它不起作用。在您更改按钮之前,轴根本不存在 function varargout = selectfloorplan(varargin) % SELECTFLOORPLAN MATLAB code for selectfloorplan.fig % SELECTFLOORPLAN, by its

这是目前所有的代码。更改按钮组中的选定按钮时会显示正确的图像,但在更改选定按钮之前不会显示图像。我希望第一个图像是FloorPlan{1},正如它在axes1_CreateFcn中所说的那样,但它不起作用。在您更改按钮之前,轴根本不存在

function varargout = selectfloorplan(varargin)
% SELECTFLOORPLAN MATLAB code for selectfloorplan.fig
%      SELECTFLOORPLAN, by itself, creates a new SELECTFLOORPLAN or raises the existing
%      singleton*.
%
%      H = SELECTFLOORPLAN returns the handle to a new SELECTFLOORPLAN or the handle to
%      the existing singleton*.
%
%      SELECTFLOORPLAN('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SELECTFLOORPLAN.M with the given input arguments.
%
%      SELECTFLOORPLAN('Property','Value',...) creates a new SELECTFLOORPLAN or raises     the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before selectfloorplan_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to selectfloorplan_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 selectfloorplan

% Last Modified by GUIDE v2.5 18-Apr-2014 11:16:48

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @selectfloorplan_OpeningFcn, ...
                   'gui_OutputFcn',  @selectfloorplan_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 selectfloorplan is made visible.
function selectfloorplan_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 selectfloorplan (see VARARGIN)

% Choose default command line output for selectfloorplan
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes selectfloorplan wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = selectfloorplan_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 structure
varargout{1} = handles.output;


% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
global FloorPlans floorplanselection
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

%Hint: place code in OpeningFcn to populate axes1
imagesc(FloorPlans{1})
floorplanselection=FloorPlans{1};
axis off
axis('image')
colormap(gray)


% --- Executes when selected object is changed in uipanel1.
function uipanel1_SelectionChangeFcn(hObject, eventdata, handles)
global FloorPlans floorplanselection
% hObject    handle to the selected object in uipanel1 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%   EventName: string 'SelectionChanged' (read only)
%   OldValue: handle of the previously selected object or empty if none was selected
%   NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)
switch get(eventdata.NewValue,'Tag')
    case 'rad1'
        axes(handles.axes1)
        imagesc(FloorPlans{1})
        floorplanselection=FloorPlans{1};
        axis off
        axis('image')
        colormap(gray)
    case 'rad2'
        axes(handles.axes1)
        imagesc(FloorPlans{2})
        floorplanselection=FloorPlans{2};
        axis off
        axis('image')
        colormap(gray)
    case 'rad3'
        axes(handles.axes1)
        imagesc(FloorPlans{3})
        floorplanselection=FloorPlans{3};
        axis off
        axis('image')
        colormap(gray)
    case 'rad4'
        axes(handles.axes1)
        imagesc(FloorPlans{4})
        floorplanselection=FloorPlans{4};
        axis off
        axis('image')
        colormap(gray)
    case 'rad5'
        axes(handles.axes1)
        imagesc(FloorPlans{5})
        floorplanselection=FloorPlans{5};
        axis off
        axis('image')
        colormap(gray)
    case 'rad6'
        axes(handles.axes1)
        imagesc(FloorPlans{6})
        floorplanselection=FloorPlans{6};
        axis off
        axis('image')
        colormap(gray)
end


% --- Executes on button press in nectbtn.
function nectbtn_Callback(hObject, eventdata, handles)
% hObject    handle to nectbtn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
selectrobot
delete(get(hObject, 'parent'));


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
mainmenu
delete(get(hObject, 'parent'));


% --- Executes during object creation, after setting all properties.
function uipanel1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to uipanel1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

图形对象的ID存储在
句柄
结构中,该结构用于引导回调之间的存储和传递。axis1不能作为输入使用的原因是它未在回调函数的作用域中定义,因此函数无法对其进行寻址。这就是为什么您的全局解决方案“修复”了这个问题


如果在
axes()
调用中将
axis1
替换为
handles.axes1
(或Axis对象标记的任何内容),则它的性能应符合您的预期。

您遇到了什么问题?解释你被困在哪里以及你尝试了什么。这看起来也有点像家庭作业的问题。我实际上改变了一些我认为是问题的东西。在每种情况下,我都将axes(axis1)语句设置为axes(axes1),以适合轴的标记。它仍然不起作用。我认为这与这些线条有关,因为轴的创建很好。我在命令窗口中遇到的第一个错误是:未定义的函数或变量“axes1”,后面是一长串其他内容。我尝试了一些有效的方法。在axes1_createFcn中,我定义了一个全局变量h。同样在该fcn中,h=gcbo。然后在SelesctionChangeFcn中,我将h定义为全局,并将所有axes1更改为h。它工作得很好,我只是希望有一种方法可以直接获得句柄,而不需要全局变量。我只是尝试了一下,它适用于选择更改功能,但当GUI第一次运行时,不会填充轴。我没有出错。如果我更改按钮,图形会更改,但在我更改按钮之前,轴是空的。我试图在axes1创建和按钮组创建中搞乱,但它没有解决问题。你必须更具体一些。用您的更改更新您的帖子,并发布完整的代码。