Problem with the use of strrep command

조회 수: 22 (최근 30일)
Ivan Mich
Ivan Mich 2023년 3월 6일
댓글: Steven Lord 2023년 3월 7일
I have a problem with strrep command. I have noticed that this command no use for cell array with square bracket character ([])
Could you please help me?
  댓글 수: 3
Askic V
Askic V 2023년 3월 6일
Here is one example with cell array with square brackets and use of strrep function:
aa = {['first'],['second']}
aa = 1×2 cell array
{'first'} {'second'}
aa
aa = 1×2 cell array
{'first'} {'second'}
aa(1) = strrep(aa(1),'first','replacement');
aa
aa = 1×2 cell array
{'replacement'} {'second'}
Can you explain what problem could be here?
DGM
DGM 2023년 3월 6일
편집: DGM 2023년 3월 6일
In that case, the square brackets have no influence over the processing.
aa = {['first'],['second']}
is the same as
aa = {'first','second'}
Since we're guessing here, other interpretations might include:
% an unintended concatenation
aa = {['first','second']}
... or
% the brackets are within the char vectors
aa = {'first [subexpr]','second [other subexpr]'}
Where either the brackets themselves are to be replaced (shouldn't be a problem), or the subexpressions therin are to be replaced (strrep would be the wrong tool).
Alternatively, my guess is that this might not even be a cellchar
% a cell of strings or string arrays might cause problems
aa = {"first" "second"}
aa = {["first" "second"]}
in which case the solution really depends on what's in the cell array, how it's arranged, and whether it should even be in a cell array.
But we'll have to wait.

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

답변 (1개)

Steven Lord
Steven Lord 2023년 3월 6일
That is correct, when called with a cell array with some char vectors in the cells as input strrep requires each element of that cell array to be a char vector. I'm using try / catch here so I can run code after the section that throws the error.
a = {'apple', [], 'cherry'}
a = 1×3 cell array
{'apple'} {0×0 double} {'cherry'}
try
strrep(a, 'e', 'E')
catch ME
fprintf("This call threw error '%s'.\n", ME.message)
end
This call threw error 'Cell elements must be character vectors.'.
Use cellfun or a for loop to replace the empty numeric elements with an empty char array.
E = cellfun(@isempty, a);
a(E) = {''}
a = 1×3 cell array
{'apple'} {0×0 char} {'cherry'}
strrep(a, 'e', 'E')
ans = 1×3 cell array
{'applE'} {0×0 char} {'chErry'}
  댓글 수: 3
Askic V
Askic V 2023년 3월 7일
Cool, thank you for the insight @Steven Lord.
Important thing to watch for!
Steven Lord
Steven Lord 2023년 3월 7일
More likely than not the code that generated the variable a did not explicitly assigned an empty vector into it, so it's easy to overlook that possibility.
a = {'apple'}
a = 1×1 cell array
{'apple'}
a{3} = 'cherry'
a = 1×3 cell array
{'apple'} {0×0 double} {'cherry'}
MATLAB needed to put something into the second cell in the variable a when I assigned 'cherry' into the third cell. It essentially chose []. In this case where I knew I wanted all the cells to have text data I might have preallocated the cell array with repmat to ensure all cells had some text data. That's not so much preallocating for memory (cells in a cell array are not required to be contiguous in memory like elements in a numeric array) but more preallocating with the expected types.
b = repmat({''}, 1, 3)
b = 1×3 cell array
{0×0 char} {0×0 char} {0×0 char}
b{1} = 'apple'
b = 1×3 cell array
{'apple'} {0×0 char} {0×0 char}
b{3} = 'cherry'
b = 1×3 cell array
{'apple'} {0×0 char} {'cherry'}
strrep(b, 'e', 'E')
ans = 1×3 cell array
{'applE'} {0×0 char} {'chErry'}

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by