String Delimiting and Array Storage

Hi, could someone tell me the best way to delimit a long single row array of characters based on a reoccurring character in the array? In this case I am using "/" to separate strings.
The biggest challenge I am having is storing delimited strings into an array because the string length is not constant.
I can have something like abc/adeht/ahdiglkjsad/asd/ds but I cannot create the following array
[adeht
ahdiglkjsad
asd
ds ]
because the strings are all different lengths and the dimensions do not match.
Can anyone recommend a solution? I need to be able to reference and analyze each string on a per character basis after delimiting. That's why I chose to try to do an array originally, however, this is not working out.
Thanks, Nikolay

 채택된 답변

Jan
Jan 2012년 10월 1일

0 개 추천

You can use a cell string:
str = 'abc/adeht/ahdiglkjsad/asd/ds';
cstr = regexp(str, '/', 'split');

댓글 수: 8

Matt Fig
Matt Fig 2012년 10월 1일
편집: Matt Fig 2012년 10월 1일
To get the results in to one array, as you show in your question:
char(cstr)
But if you are analyzing each sub-string separately, a cell array is fine and perhaps preferred for storage.
Jan
Jan 2012년 10월 1일
편집: Jan 2012년 10월 1일
After char(cstr) you get a CHAR matrix padded with spaces. There is no way to avoid the padding, because a matrix must be rectangular amd all rows must have the same number of elements.
Matt Fig
Matt Fig 2012년 10월 1일
Yes, I should not have left that out.
Nikolay Rodionov
Nikolay Rodionov 2012년 10월 1일
Thanks, this works great but in using the char() function I am encountering the same difficulty with padding as I was before. Is there anyway to get the length of a string in, lets say, array = char(cstr), array(1,:) that is not padding? If I use a simple length function, then it tells me that the row length is 11 characters, even though there are only 3 letters .
Thanks again!
Matt Fig
Matt Fig 2012년 10월 1일
편집: Matt Fig 2012년 10월 1일
Why not just work with cstr instead??
length(cstr{1}) % Find the length of the first string
cellfun('length',cstr) % Or all lengths at once.
Nikolay Rodionov
Nikolay Rodionov 2012년 10월 1일
I was wondering because I am simply more familiar with arrays. I'll try working with cells though. Is there a way for me to reference portions of a cell's contents, such as characters 1:3?
This is all part of a nucleotide sequence analysis script, I need to be able to parse the delimited sequences and store them into external cells or arrays.
C = {'asdlfdjsadf','awwretjrdf'};
C{2}(4:5)
Nikolay Rodionov
Nikolay Rodionov 2012년 10월 1일
Thank you so much!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

질문:

2012년 10월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by