How to obtain data from editbox using a pushbutton?

조회 수: 10 (최근 30일)
Jan Donyada
Jan Donyada 2011년 8월 31일
Hi all, My GUI (called tts2) has an editbox and a pushbutton. By pushing the button, the pushbutton callback is supposed to scan the editbox and return the number of words written. But i'm not sure how to code the pushbutton callback to obtain the data it needs from the editbox function. Here's the code for the callback sections of the editbox and pushbutton:
function tts2_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
function varargout = tts2_OutputFcn(hObject, eventdata, handles) ;
varargout{1} = handles.output;
function editbox_Callback(hObject, eventdata, handles)
function editbox_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function pushbutton1_Callback(hObject, eventdata, handles)
words = get(hObject, 'string');
wordsarray = textscan (words, '%s');
max_words = length(wordsarray)
disp (wordsarray)

채택된 답변

Paulo Silva
Paulo Silva 2011년 8월 31일
words = get(handles.edit, 'String'); %edit is the name of the edit box
if you are using GUIDE the names of the editboxes are done consecutively (edit1,edit2,edit3...) so if you only have one editbox the name is edit1 unless you changed it.
words = get(handles.edit1, 'String');
  댓글 수: 2
Jan Donyada
Jan Donyada 2011년 8월 31일
ahh ok.
function pushbutton1_Callback(hObject, eventdata, handles)
words = get(handles.editbox, 'string');
wordsarray = textscan (words, '%s'); %this splits up the string into an array
max_words = length(wordsarray) %this is supposed to count the number of words in the array
disp (max_words)
now, no matter how many words are keyed into the editbox, the only value returned is 1, when it should actually output the number of words in the input string. i know textscan splits the string into an array of words and i suspect that its only counting one dimension (only the row or only the column, because it stores it in a 1xK array, where k is the number of words, i think). how do i make disp() display the other dimension? or is there another function to output K from a 1xK array?
Paulo Silva
Paulo Silva 2011년 8월 31일
wordsarray is actually a cell not one array, do this:
wordscell = textscan (words, '%s');
nwords=cellfun(@numel,wordscell);
disp (nwords)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by