MATLAB Answers

How can I create new edit boxes based on user input, where are the callbacks generated????

4 views (last 30 days)
matlabuser12
matlabuser12 on 8 May 2014
Commented: Roberto on 8 May 2014
I have an editbox and a pushbutton. IN the edit box you put a number say 10, and then you hit the pushbutton. This tells the code to make two columns of edit boxes on the gui each with 10 boxes in them. Where are these new boxes defined?? I want to have the user enter data into them, but then how do i tell the code to do things with that data? I have thought about adding a finished entering data button that will do some action, but I am not sure what that action is since I don't know what to use to reference the new edit boxes. The code is below
if true
% function varargout = Testgui(varargin)
% TESTGUI MATLAB code for Testgui.fig
% TESTGUI, by itself, creates a new TESTGUI or raises the existing
% singleton*.
%
% H = TESTGUI returns the handle to a new TESTGUI or the handle to
% the existing singleton*.
%
% TESTGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TESTGUI.M with the given input arguments.
%
% TESTGUI('Property','Value',...) creates a new TESTGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Testgui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Testgui_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 Testgui
% Last Modified by GUIDE v2.5 08-May-2014 06:50:51
% Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @Testgui_OpeningFcn, ... 'gui_OutputFcn', @Testgui_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 Testgui is made visible. function Testgui_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 Testgui (see VARARGIN)
% Choose default command line output for Testgui handles.output = hObject;
% Update handles structure guidata(hObject, handles);
% UIWAIT makes Testgui wait for user response (see UIRESUME) % uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line. function varargout = Testgui_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 button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) Numberofevents = str2double(get(handles.edit1,'String')); for i=1:Numberofevents textEl(i)=uicontrol('Style','edit','background',[1 1 1],'position',[100 ((i+1)*30)+30,100,20]); textEl(i)=uicontrol('Style','edit','background',[1 1 1],'position',[300 ((i+1)*30)+30,100,20]); end
function edit1_Callback(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text % str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties. function edit1_CreateFcn(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end
% --- Executes on button press in dataentered. function dataentered_Callback(hObject, eventdata, handles) % hObject handle to dataentered (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
end

Answers (2)

Roberto
Roberto on 8 May 2014
Or you can try saving the handles after creating the control! for example:
function button1_fnc_callback(hObject,eventData,handles)
% button pushed
for i = 1 : 10
handles.myEdits(i) = uicontrol(handles.figure, 'Style','edit');
end
guidata(handles.figure, handles);
after that, the button that uses the data could be like this:
function button2_fnc_callback(hObject,eventData,handles)
% button pushed
for i = numel(handles.myEdits)
myData(i) = eval(get(handles.myEdits(i),'String'));
end
  3 Comments
Roberto
Roberto on 8 May 2014
NO!!! this will create an array named myEdits like this:
myEdits =
[ 200.1030 ]
[ 200.1021 ]

Sign in to comment.


Roberto
Roberto on 8 May 2014
Check this video tutorials! on building GUIs. Also you can try this function I wrote, to easily add controls to a GUI. To access the data here's an example:
% lets create the control (or see the exmple in the layControl function
handles.mycontrol = uicontrol(handles.figure,'Style','edit','String','Edit me here!');
set(handles.mycontrol,'Position',[10 10 180, 30])
% to get the value of the control
strValue = get(handles.mycontrol,'String');
evaluatedValue = eval(strValue);
I hope this will help you!
  1 Comment
matlabuser12
matlabuser12 on 8 May 2014
This just adds a control to the gui I think, which isn't what I am trying to do. I can add the edit boxes no problem, but I don't understand how I then call their entries independently to use later since they have no callback definitions that i can find

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by