|
From: | Rik |
Subject: | Re: character matrix inputs to string functions |
Date: | Thu, 20 Feb 2020 10:22:43 -0800 |
On 02/20/2020 09:00 AM,
address@hidden wrote:
There is a much larger issue which should be resolved and that is how character matrix inputs should be handled by all string functions. Consider this example, octave:1> cstr = { 'Hello World' ; 'Goodbye World '} cstr = { [1,1] = Hello World [2,1] = Goodbye World } octave:2> strrep (cstr, 'World', 'Jane') ans = { [1,1] = Hello Jane [2,1] = Goodbye Jane } This does just what you would think. Now try the same thing with a character matrix. octave:3> chmat = char ('Hello World', 'Goodbye World') chmat = Hello World Goodbye World octave:4> strrep (chmat, 'World', 'Jane') ans = Hello World Goodbye World There is no substitution because the internal algorithm sees a string that is "HGeolo...." In any case, the average user is going to be quite surprised by the apparent failure of the strrep function. Restricting character input to be a row vector (1xN) restores the correct behavior of the function. octave:5> strrep (chmat(1,:), 'World', 'Jane') ans = Hello Jane So, I think we (Octave community) need to make a decision about how we want to handle character matrix inputs and then propagate this change to all of the m-files in scripts/strings. One obvious possibility is simply to follow Matlab and increase the level of input validation to reject character matrices. The validation code is pretty simple if (ischar (input)) if (! isrow (input)) error ("fcn_name: input must be a character string or cell array of strings"); endif elseif (iscellstr (input)) ... else error ("fcn_name: input must be a character string or cell array of strings"); endif But Octave does try to see itself as a superset of Matlab. We don't have to follow them slavishly. In this case we could change the input validation to detect the character matrix and call the function recursively. For example, this code converts the character matrix to a cell array of string, executes strrep, and converts the output back to a character matrix. if (ischar (input)) if (! isrow (input)) retval = char (strrep (cellstr (input), pattern, replacement))); return; endif endif And it works, octave:7> char (strrep (cellstr (chmat), 'World', 'Jane')) ans = Hello Jane Goodbye Jane Anyone want to comment on which approach they like and why? --Rik |
[Prev in Thread] | Current Thread | [Next in Thread] |