Receive an error when using an empty string array input in the "strrep" function.

When I use an empty string array as any of the three inputs to the "strrep" function, the function output is different in comparison to the "replace" function. However, their respective documentation pages appear to describe the same functionality.
As a particular example, when I try to run the following MATLAB code using the "strrep" function, I receive the following error message:
strrep(strings(1,0),["A","B"],["C","D"])
Error using strrep
All nonscalar inputs must be the same size.
However, if I use exactly the same input arguments for the "replace" function, there is no error message:
replace(strings(1,0),["A","B"],["C","D"])
ans =
1×0 empty string array
Is there a workaround to avoid this error message appearing when using "strrep"?

 Risposta accettata

In response to the first question posed, this is expected behavior.
The "replace" documentation does not describe any input size constraints for the function:
However, for the "strrep" function, if any input argument is a nonscalar string array or cell array of character vectors, then the other input arguments must have compatible sizes.
Please refer to the "strrep" documentation page for further detail on such input size constraints:
For your example code provided, the error message appears because the three input variables to "strrep" must all have the same size as the other two inputs, which are both 1x2 string arrays.
In order to avoid this error message, there are two options:
1. Replace the first input in "strrep" with "strings(1,2)".
2. Replace both the second and third inputs to "strrep" with a single empty string:\n
strrep(strings(1,0),[""],[""])

Più risposte (0)

Categorie

Prodotti

Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by