What is the alternative for startsWith function in Matlab 2013?

조회 수: 11 (최근 30일)
Kapa Kudaibergenov
Kapa Kudaibergenov 2019년 11월 8일
답변: Akansha Saxena 2020년 10월 21일
TF = startsWith(names_cell,pattern)
I have a snippet of code written above. The names_cell is a cell array and pattern is a string array.
I need to find an alternative in Matlab R2013b, where there is no such a function.

답변 (2개)

Walter Roberson
Walter Roberson 2019년 11월 8일
temp = arrayfun(@(S) strncmp(names_cell, S, length(S)), pattern, 'uniform', 0);
nd = ndims(temp{1}) + 1;
TF = or(cat(nd, temp{:}), nd);
This is designed to work no matter how many dimensions names_cell has. The output should be the same size as names_cell is.
The code can be simplified for the more common case where pattern is a character vector or scalar string array.
  댓글 수: 2
Kapa Kudaibergenov
Kapa Kudaibergenov 2019년 11월 11일
Why doesn't it create the array of the same size as names_cell? It should create e.g. 1x12 array with 1s and 0s, instead it creates 9x12 array with only 1s.
Walter Roberson
Walter Roberson 2019년 11월 11일
What is size(names_cell) and size(pattern) ?
At one point some functions shifted from giving a row vector result in some cases to giving a column vector result instead; I would need to test further on that point. I am not sure I have R2013b still installed though.

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


Akansha Saxena
Akansha Saxena 2020년 10월 21일
TF = cellfun(@(x) strncmpi(x, pattern, length(pattern)),names_cell);

카테고리

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

제품


릴리스

R2013b

Community Treasure Hunt

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

Start Hunting!

Translated by