필터 지우기
필터 지우기

Dividing a string into sub-strings using a for-loop

조회 수: 1 (최근 30일)
Laura Nielsen
Laura Nielsen 2021년 1월 13일
댓글: Cris LaPierre 2021년 1월 13일
Hi,
I have a string as shown below:
FilesID='data_190830_102602.mem,data_191030_141002.mem,data_191230_101612.mem'
I want to extract the characters between the commas - each of them is unique, as they are based on timestamps.
I have tried to built a for-loop as seen below, however, it seems not to terminate and a fault occurs;
"Index exceeds the number of array elements"
with respect to this line:
word{j} = FilesID(qnew:qnew+21)
Here is the entire code:
j = 1;
for a=1:length(FilesID)
for q = (j-1):22:length(FilesID)
qnew=q+a;
word{j} = FilesID(qnew:qnew+21)
a=a+1;
j = j+1;
end
end
Thanks

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 1월 13일
Why a for loop? I would use strsplit
FilesID='data_190830_102602.mem,data_191030_141002.mem,data_191230_101612.mem';
prts = strsplit(FilesID,',')
prts = 1x3 cell array
{'data_190830_102602.mem'} {'data_191030_141002.mem'} {'data_191230_101612.mem'}
  댓글 수: 3
Cris LaPierre
Cris LaPierre 2021년 1월 13일
I'm sure it is. I'll let you explore that.
Cris LaPierre
Cris LaPierre 2021년 1월 13일
If it helps, what you are looking for might be called parsing a string, or string tokenization. See if this example is more along the lines of what you needed.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by