how to match sub string with main string in a cell array??
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi,
I have a cell array as below
cell_arr= ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/212091/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/212091/image.png)
sub string as b = {'hard'}
If sub string matches with column1 i.e., cell_arr{:,1}, only that particular column2 has the value else it must be empty. i tried a lot with this.
out = ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/212092/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/212092/image.png)
I am attaching the sample code here
a = {'hard';'smart';'hard drive';'smart work';'hark disk';'hard drive'};
b = {0.1;0.2;0.4;0.7;0.8;0.9};
cell_arr=[a,b];
out= cell_arr(strcmp(cell_arr,'hard'),:);
Please help me if any idea on this.
댓글 수: 1
madhan ravi
2019년 4월 5일
15 questions asked 46 answers answered yet you post a photo instead of code.
채택된 답변
Jan
2019년 4월 5일
편집: Jan
2019년 4월 5일
With modern Matlab versions >= R2016b:
a = {'hard';'smart';'hard drive';'smart work';'hark disk';'hard drive'};
b = {0.1;0.2;0.4;0.7;0.8;0.9};
c = [a,b];
% If the substring should be existing anywhere:
c(~contains(c(:,1), 'hard'), 2) = {[]};
% or if the substring should appear at the start:
c(~startsWith(c(:,1), 'hard'), 2) = {[]};
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!