How to concatenate strings from multiple push button?
Show older comments
Hey, I want a gui with four push buttons. Say, push button 1 gives an output of A, pb2 B, pb3 C and pb4 D. What I want to do is, I like to concatenate each letter in one string. That is, everytime a push button is pressed, it will add the new letter to the string. For example, pb1 pb2 pn3 pb4 the output should be A B C D thanks!
1 Comment
Mark Jecel Rapir
on 9 Aug 2018
Answers (2)
KSSV
on 8 Aug 2018
pb1 = 'A' ;
pb2 = 'B' ;
pb3 = 'C' ;
pb4 = 'D' ;
S = [pb1,' ',pb2,' ',pb3,' ',pb4] ;
Walter Roberson
on 9 Aug 2018
Edited: Walter Roberson
on 9 Aug 2018
Initialize:
handles.currentstring = '';
guidata(hObject, handles)
Then you can use the same callback code for all four pushbuttons:
function pushbuttonA_callback(hObject, event, handles)
handles.currentstring = [handles.currentstring hObject.String];
guidata(hObject, handles);
2 Comments
Mark Jecel Rapir
on 9 Aug 2018
Walter Roberson
on 9 Aug 2018
If you are using GUIDE, then put
handles.currentstring = '';
into the OpenFcn_Callback, and make sure that callback ends with
guidata(hObject, handles)
Then, build the four pushbuttons, setting their String values to 'A', 'B', 'C', and 'D'. Configure the Callback associated with each one of them to contain
handles.currentstring = [handles.currentstring hObject.String];
guidata(hObject, handles);
in all four cases.
You would need slightly different code if you were using R2014a or earlier.
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!