Skip to content

Commit c5d4d48

Browse files
committed
tb_optparse had a bug when parsing for LineSpecs when valid characters were repeated. Added unit test to ensure that parsing works correctly. Also cleaned up code to avoid code advisor warnings.
Closes #13.
1 parent 56b4a76 commit c5d4d48

File tree

3 files changed

+55
-28
lines changed

3 files changed

+55
-28
lines changed

test/toolbox/ttb_optparse.m

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ function validLineSpecString(testCase, pLineSpecValid)
7070
function validLineSpecRepeated(testCase)
7171
%validLineSpecRepeated Test for having repeated tokens
7272

73-
% This test case fails for now, because of a bug that is also
74-
% triggered by chapter 5 code.
75-
testCase.assumeFail("Waiting for fix");
76-
7773
repeatedColor = ':kk';
7874
[~,~,actualLineSpec] = tb_optparse(struct, {repeatedColor});
7975
testCase.verifyEqual(actualLineSpec{1}, repeatedColor);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
function isLS = isLineSpec(s)
2+
%ISLINESPEC Determine if input character array is a linespec
3+
% See LineSpec definition in https://www.mathworks.com/help/matlab/ref/plot.html
4+
% documentation.
5+
6+
% Copyright 2025 Peter Corke, Witold Jachimczyk, Remo Pillat
7+
8+
%#ok<*AGROW>
9+
10+
s = convertStringsToChars(s);
11+
12+
if ~ischar(s)
13+
isLS = false;
14+
return;
15+
end
16+
17+
s2 = '';
18+
19+
% get color
20+
removeMask = false(1, length(s));
21+
[b,e] = regexp(s, '[rgbcmywk]');
22+
for j = 1:numel(b)
23+
s2 = [s2,s(b(j):e(j))];
24+
removeMask(b(j):e(j)) = true;
25+
end
26+
s(removeMask) = [];
27+
28+
% get line style
29+
removeMask = false(1, length(s));
30+
[b,e] = regexp(s, '(--)|(-.)|-|:');
31+
for j = 1:numel(b)
32+
s2 = [s2,s(b(j):e(j))];
33+
removeMask(b(j):e(j)) = true;
34+
end
35+
s(removeMask) = [];
36+
37+
% get marker style
38+
removeMask = false(1, length(s));
39+
[b,e] = regexp(s, '[o\+\*\.xsd\^v><ph]');
40+
for j = 1:numel(b)
41+
s2 = [s2,s(b(j):e(j))];
42+
removeMask(b(j):e(j)) = true;
43+
end
44+
s(removeMask) = [];
45+
46+
% This is a LineSpec if all characters from s have been removed
47+
isLS = isempty(s);
48+
49+
end
50+

toolbox/tb_optparse.m

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,9 @@
8080

8181
% Copyright 2022-2023 Peter Corke, Witold Jachimczyk, Remo Pillat
8282

83-
function [opt,others,ls] = tb_optparse(in, argvIn, cls)
83+
%#ok<*AGROW>
8484

85-
if nargin == 1
86-
argv = {};
87-
end
85+
function [opt,others,ls] = tb_optparse(in, argvIn, cls)
8886

8987
if nargin < 3
9088
cls = [];
@@ -307,28 +305,11 @@
307305
ls = [];
308306
for i=1:length(arglist)
309307
s = arglist{i};
310-
if ~ischar(s)
311-
continue;
312-
end
313-
% get color
314-
[b,e] = regexp(s, '[rgbcmywk]');
315-
s2 = s(b:e);
316-
s(b:e) = [];
317-
318-
% get line style
319-
[b,e] = regexp(s, '(--)|(-.)|-|:');
320-
s2 = [s2 s(b:e)]; %#ok<*AGROW>
321-
s(b:e) = [];
322-
323-
% get marker style
324-
[b,e] = regexp(s, '[o\+\*\.xsd\^v><ph]');
325-
s2 = [s2 s(b:e)]; %#ok<*NASGU>
326-
s(b:e) = [];
327-
308+
328309
% found one
329-
if isempty(s)
310+
if rvc.internal.isLineSpec(s)
330311
ls = arglist{i};
331-
arglist(i) = [];
312+
arglist(i) = [];
332313
break;
333314
end
334315
end

0 commit comments

Comments
 (0)