Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

GUI setting default value to text box

1 view (last 30 days)
Peng Zhao
Peng Zhao on 27 Jun 2019
Closed: Peng Zhao on 28 Jun 2019
I am trying to set a default to the Text box or the slider, but this is not working as the text box does not show the default value:
function varargout = GPEC_Task_Generator(varargin)
% GPEC_Task_Generator MATLAB code for GPEC_Task_Generator.fig
% GPEC_Task_Generator, by itself, creates a new GPEC_Task_Generator or raises the existing
% singleton*.
%
% H = GPEC_Task_Generator returns the handle to a new GPEC_Task_Generator or the handle to
% the existing singleton*.
%
% GPEC_Task_Generator('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GPEC_Task_Generator.M with the given input arguments.
%
% GPEC_Task_Generator('Property','Value',...) creates a new GPEC_Task_Generator or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GPEC_Task_Generator_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GPEC_Task_Generator_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 GPEC_Task_Generator
% Last Modified by GUIDE v2.5 27-Jun-2019 13:56:22
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GPEC_Task_Generator_OpeningFcn, ...
'gui_OutputFcn', @GPEC_Task_Generator_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 GPEC_Task_Generator is made visible.
function GPEC_Task_Generator_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 GPEC_Task_Generator (see VARARGIN)
% Choose default command line output for GPEC_Task_Generator
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GPEC_Task_Generator wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GPEC_Task_Generator_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 on slider movement.
function Num_Task_Callback(hObject, eventdata, handles)
% hObject handle to Num_Task (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Num_Task_Val = round(hObject.Value);
set(handles.Num_Task, 'Value', Num_Task_Val);
set(handles.Num_Task_Disp, 'String', Num_Task_Val);
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% --- Executes during object creation, after setting all properties.
function Num_Task_CreateFcn(hObject, eventdata, handles)
% hObject handle to Num_Task (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes during object creation, after setting all properties.
function Num_Task_Disp_CreateFcn(hObject, eventdata, handles)
% hObject handle to Num_Task_Disp (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
Num_Task_Disp_Val = str2double(get(hObject,'string'))
if isnan(Num_Task_Disp_Val)
set(handles.Num_Task_Disp, 'String', '5');
end
% --- Executes on button press in Next_Button.
function Next_Button_Callback(hObject, eventdata, handles)
% hObject handle to Next_Button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
disp('Next')
% --- Executes on button press in Cancel_Button.
function Cancel_Button_Callback(hObject, eventdata, handles)
% hObject handle to Cancel_Button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
disp('Cancel')
close(GPEC_Task_Generator)
Error:
Struct contents reference from a non-struct array object.
Error in GPEC_Task_Generator>Num_Task_Disp_CreateFcn (line 108)
set(handles.Num_Task_Disp, 'String', '5');
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in GPEC_Task_Generator (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)GPEC_Task_Generator('Num_Task_Disp_CreateFcn',hObject,eventdata,guidata(hObject))
  5 Comments
Rik
Rik on 27 Jun 2019
If you set a breakpoint at that line, what are the sizes of the variables involved? handles should be scalar struct, and the Num_Task_Disp field should be a scalar uicontrol object handle.
After fixing this, you could also consider replacing that line with the line below
set(hObject, 'String', '5')
Peng Zhao
Peng Zhao on 28 Jun 2019
I was able to set the default within the opening function:
% --- Executes just before GPEC_Task_Generator is made visible.
function GPEC_Task_Generator_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 GPEC_Task_Generator (see VARARGIN)
Num_Task_Val_Default = 1;
set(handles.Num_Task, 'Value', Num_Task_Val_Default);
set(handles.Num_Task_Disp, 'String', Num_Task_Val_Default);
% Choose default command line output for GPEC_Task_Generator
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
But thanks for helping.

Answers (0)

This question is closed.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!