How can I make a string entered in an "Edit Text" box become a variable?
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I'm trying to make a game of some sort where one part involves typing in text in an "edit text" box, and another user typing in text in another "edit text" box. In short, if the text matches, the 2nd player wins the game.
Right now, I'm having trouble trying to figure out how to get the string in the "Edit Text" box to be recognized as a variable from which it will be easy to compare other strings to.
What I've tried:
function GueText_CreateFcn(hObject, eventdata, handles) % hObject handle to GueText (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
B = get(GueText,'String');
Which doesn't work. I think I'm not using the "get" command properly and am not entering the proper handle.
Any ideas? Thanks for any help you can provide.
0 Commenti
Risposta accettata
Matt Fig
il 18 Nov 2012
Modificato: Matt Fig
il 18 Nov 2012
Here is one example that shows the process of manipulating data from edit boxes.
function [] = gui_edits()
% How to use strings from edit boxes.
S.fh = figure('name','editbox game',...
'menubar','none',...
'numbert','off',...
'pos',[100 100 300 150]);
S.ed(1) = uicontrol('Style','edit',...
'Units','pix',...
'Position',[10 10 130 130],...
'CallBack',@callb,...
'String','Player 1');
S.ed(2) = uicontrol('Style','edit',...
'Units','pix',...
'Position',[150 10 130 130],...
'CallBack',@callb,...
'String','Player 2');
movegui('center')
S.P1 = '';
S.P2 = '';
guidata(S.fh,S) % Store the structure.
uicontrol(S.ed(1)) % put the focus on player 1s box.
function [] = callb(varargin)
% Callback for the edit boxes
S = guidata(gcbf); % Get the structure.
if gcbo==S.ed(1) % If player 1 called.
S.P1 = get(S.ed(1),'string'); % Store the string!
set(S.ed(1),'string','Waiting for Player 2!')
uicontrol(S.ed(2)) % Put focus on player 2.
else
TF = strcmp(S.P1,get(S.ed(2),'string'));
if TF
set(S.ed,'string','Player 2 wins!')
else
set(S.ed,'string','Player 1 wins!')
end
end
guidata(S.fh,S)
2 Commenti
Matt Fig
il 25 Nov 2012
Chris comments:
Oh wow. Thanks. Sorry I took so long to reply. Just a couple questions: is the process the exact same if you make the GUI first from GUIDE and then enter the code? Also is it better practice to make the boxes from uicontrols as opposed to using the GUIDE? I'm trying to get used to transferring code into a more user friendly environment (and trying to develop this game as a simple example).
Matt Fig
il 25 Nov 2012
Modificato: Matt Fig
il 25 Nov 2012
The process is not much different between programmed GUIs and GUIs made with GUIDE. I much prefer to program GUIs than to use GUIDE because of the level of control and the fact that GUIDE can do weird things from time to time. I have had (and seen others have) several complex GUIs lost or damaged nearly to loss because of GUIDE glitches, so I gave it up. This isn't the norm, but it can be pretty awful to experience.
You can use similar techniques either way.
If your question has been answered, please accept the answer.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!