我已经创建了一个GUI,可以根据ui面板中提供的一些标准,使用单选按钮作为选项,估算构建房屋所涉及的材料和价格。问题是,除非首先播放单选按钮,否则按下GUI的go按钮会引发错误。我无法将我的按钮留在GUI运行时的选择上。如何运行.m文件然后点击.fig而不必触摸任何内容(除了填写方形素材编辑框)?
function uibuttongroup3_SelectionChangedFcn(hObject, eventdata, handles)
% hObject handle to the selected object in uibuttongroup3
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
tag = eventdata.NewValue;
switch get(tag,'Tag')
case 'OneHalfBath'
BathRoom = 1
case 'TwoHalfBath'
BathRoom = 2
case 'ThreeHalfBath'
BathRoom = 3
end;
if BathRoom == 1
BathWall = 30
BathSq = 36
elseif BathRoom == 2
BathWall = 48
BathSq = 72
elseif BathRoom == 3
BathWall = 66
BathSq = 108
end;
setappdata(handles.uibuttongroup3,'BathWall',BathWall);
setappdata(handles.uibuttongroup3,'BathSq',BathSq);
setappdata(handles.uibuttongroup3,'BathRoom',BathRoom);
以上是带有单选按钮的四个UI面板之一,仅供参考。所有的写法大致相同。 go按钮回调的片段如下所示。
function GoBut_Callback(hObject, eventdata, handles)
% hObject handle to GoBut (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
run('UrlPrice.m');
BathRoom = getappdata(handles.uibuttongroup3,'BathRoom');
BathSq = getappdata(handles.uibuttongroup3,'BathSq');
BathWall = getappdata(handles.uibuttongroup3,'BathWall');
% Floor = get(handles.Floor,'value')
HomeName = get(handles.HomeName,'string');
Base = get(handles.BaseSqFt, 'string');
BaseSqFt = str2num(Base);
TotSqFt = BaseSqFt .* Floor;
Perim = (((BaseSqFt .^ .5) .* 4) .* Floor);
Perim2 = (BaseSqFt .^ .5) .*4;
Perim3 = (BaseSqFt .^ .5);
WallLnFt_01 = Perim + (BedWall .* 2) + (BathWall .* 2); % double side wall
WallLnFt_02 = Perim + BedWall + BathWall; % one side wall
IntDoor = (BedRoom .* 2) + (BathRoom) + 2;
Tile = ceil(BathSq/17.44287);
Thinset = BathRoom + 1;
Index of element to remove exceeds matrix dimensions.
错误讯息:
Index of element to remove exceeds matrix dimensions.
Error in cur2str (line 75)
t(1,:) = [];
Error in HomeBuilding>GoBut_Callback (line 267)
EstMat = cur2str(EstMat1,2);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in HomeBuilding (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)HomeBuilding('GoBut_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
答案 0 :(得分:0)
您使用setappdata
存储变量,以便以后在uibuttongroup3_SelectionChangedFcn
中使用。问题是这只在该功能中完成。这意味着在调用uibuttongroup3_SelectionChangedFcn
之前不会存储变量,只有在按照您所说的方式播放单选按钮时才会发生这种情况。
GUI打开时,尚未定义变量。因此,您应该使用一些默认值在OpeningFcn
中定义三个变量,或者确保用户在点击之前设置按钮。