Convert cell array to character array including string manipulation

조회 수: 9 (최근 30일)
Florian
Florian 2019년 1월 20일
편집: madhan ravi 2019년 1월 20일
Hi all,
I have a n*1 cell array where each value is a combination of drive letter, folder path and file name and file extension such as:
{'C:TEMP\filename1.ext'}
{'C:TEMP\filename2.ext'}
{'C:TEMP\filename3.ext'}
...
Each row has a unique file name as shown above.
Now I would like to convert this cell array into a character array where each string is reduced to the file name only withouth path and file extension:
'filename1'
'filename2'
'filename3'
...
Does anybody know how to accomplish this?
Best regards,
Florian

채택된 답변

madhan ravi
madhan ravi 2019년 1월 20일
R=cell(size(s)); % s is your cell array
for k=1:numel(s)
S=strsplit(s{k},{'\','.'});
R{k}=S{2};
end
Result=string(R)

추가 답변 (1개)

madhan ravi
madhan ravi 2019년 1월 20일
Simpler:
R=regexp(s,'(?<=\\)\w+(?=[.])','match'); % s is your cell array
Result=string(R)
  댓글 수: 2
Florian
Florian 2019년 1월 20일
Hi Madhan,
the result of this operation is indeed a character array, but it is only filled with '1' in each cell.
madhan ravi
madhan ravi 2019년 1월 20일
편집: madhan ravi 2019년 1월 20일
What do you mean ? see the example below:
s=cell(3,1);
s{1}='C:TEMP\filename1.ext';
s{2}='C:\TEMP\filename2.ext';
s{3}='C:TEMP\filename3.ext';
R=regexp(s,'(?<=\\)\w+(?=[.])','match');
Result=string(R)
Gives:
Result =
3×1 string array
"filename1"
"filename2"
"filename3"

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by