How to count length of array?

조회 수: 5 (최근 30일)
Jan Donyada
Jan Donyada 2011년 8월 31일
hi all, im using GUIDE to form my gui.
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?

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 8월 31일
wordsarray is a cell array so the length is 1. try length(wordsarray{1})
  댓글 수: 2
Jan Donyada
Jan Donyada 2011년 8월 31일
thanks guys. can u explain what the addition does? jus trying to understand what i'm coding.
Fangjun Jiang
Fangjun Jiang 2011년 8월 31일
textscan() returns result in cell array. {} is used to reference the element of the cell array, just like () is used to reference double array. Since you have only one '%s' in your format string for textscan(), so your result is always in wordsarray{1}, which itself is a cell array of strings. wordsarray{1}{1} will be the first word.

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

추가 답변 (1개)

Rick Rosson
Rick Rosson 2011년 8월 31일
Hi Jan,
Please try the following:
max_words = length(wordsarray{1});
HTH.
Rick
  댓글 수: 1
Jan Donyada
Jan Donyada 2011년 8월 31일
thanks guys. can u explain what the addition does? jus trying to understand what i'm coding.

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

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by