Skip to content

Commit 956a636

Browse files
committed
add ability to specify file identifier
add default behaviour to prefix the line with the name of the passed variable tidy up unit tests to support this, also sort some errors with trprint called instead of trprint2
1 parent 4fa8b69 commit 956a636

File tree

5 files changed

+50
-18
lines changed

5 files changed

+50
-18
lines changed

RTBPose.m

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -555,10 +555,12 @@
555555
% - P can be instances of SO3 or SE3.
556556
%
557557
% See also RTBPose.print, trprint.
558+
args = {varargin{:}, 'label', inputname(1)};
559+
558560
if nargout == 0
559-
print(obj, varargin{:});
561+
print(obj, args{:});
560562
else
561-
out = print(obj, varargin{:});
563+
out = print(obj, args{:});
562564
end
563565
end
564566

@@ -763,15 +765,20 @@ function disp(obj)
763765
% type.
764766
%
765767
% See also trprint, trprint2.
768+
if isa(obj, 'SO2') || isa(obj, 'SE2')
769+
printfn = @trprint2;
770+
else
771+
printfn = @trprint;
772+
end
766773
if nargout == 0
767774
for T=obj
768-
trprint(T.T, varargin{:});
775+
printfn(double(T), varargin{:});
769776
end
770777
else
771778
out = '';
772779

773780
for T=obj
774-
out = strvcat(out, trprint(T.T, varargin{:}));
781+
out = strvcat(out, printfn(double(T), varargin{:}));
775782
end
776783
end
777784
end

trprint.m

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
%
99
% S = TRPRINT(T, OPTIONS) as above but returns the string.
1010
%
11-
% TRPRINT T is the command line form of above, and displays in RPY format.
11+
% TRPRINT T OPTIONS is the command line form of above.
12+
1213
%
1314
% Options::
1415
% 'rpy' display with rotation in ZYX roll/pitch/yaw angles (default)
@@ -19,6 +20,7 @@
1920
% 'radian' display angle in radians (default is degrees)
2021
% 'fmt', f use format string f for all numbers, (default %g)
2122
% 'label',l display the text before the transform
23+
% 'fid',F send text to the file with file identifier F
2224
%
2325
% Examples::
2426
% >> trprint(T2)
@@ -27,6 +29,9 @@
2729
% >> trprint(T1, 'label', 'A')
2830
% A:t = (0,0,0), RPY/zyx = (-0,0,-0) deg
2931
%
32+
% >> trprint B euler
33+
% t = (0.629, 0.812, -0.746), EUL = (125, 71.5, 85.7) deg
34+
%
3035
% Notes::
3136
% - If the 'rpy' option is selected, then the particular angle sequence can be
3237
% specified with the options 'xyz' or 'yxz' which are passed through to TR2RPY.
@@ -59,18 +64,24 @@
5964

6065
function out = trprint(T, varargin)
6166

62-
if ischar(T)
63-
% command form: trprint T
64-
trprint( evalin('caller', T) );
65-
return;
66-
end
67-
6867
opt.fmt = [];
6968
opt.mode = {'rpy', 'euler', 'angvec'};
7069
opt.radian = false;
71-
opt.label = '';
70+
opt.label = [];
71+
opt.fid = 1;
7272

7373
[opt,args] = tb_optparse(opt, varargin);
74+
75+
if ischar(T)
76+
% command form: trprint T args
77+
opt.label = T;
78+
trprint( double(evalin('caller', T)), 'setopt', opt );
79+
return;
80+
end
81+
82+
if isempty(opt.label)
83+
opt.label = inputname(1);
84+
end
7485

7586
s = '';
7687

@@ -92,7 +103,9 @@
92103

93104
% if no output provided then display it
94105
if nargout == 0
95-
disp(s);
106+
for row=s'
107+
fprintf(opt.fid, '%s\n', row);
108+
end
96109
else
97110
out = s;
98111
end
@@ -106,7 +119,7 @@
106119
s = '';
107120
end
108121
if ~isrot(T)
109-
s = strcat(s, sprintf('t = (%s),', vec2s(opt.fmt, transl(T)')));
122+
s = [s, sprintf('t = (%s),', vec2s(opt.fmt, transl(T)'))];
110123
end
111124

112125
% print the angular part in various representations

unit_test/SO2Test.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,10 @@ function display_test(tc)
198198
R = SO2( 0.3 );
199199

200200
R.print
201-
trprint(R) % old style syntax
201+
trprint2(R) % old style syntax
202202

203203
R.plot
204-
trplot(R) % old style syntax
204+
trplot2(R) % old style syntax
205205

206206
R2 = SO2(0.6);
207207
R.animate

unit_test/Transformations2Test.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ function trprint2_test(tc)
323323
tc.verifyTrue(isa(s, 'char') );
324324
tc.verifyEqual(size(s,1), 1);
325325

326-
s = trprint(a);
326+
s = trprint2(a);
327327
tc.verifyClass(s, 'char');
328328
tc.verifyEqual(size(s,1), 1);
329329

@@ -334,7 +334,7 @@ function trprint2_test(tc)
334334
a = cat(3, a, a, a);
335335
trprint2(a);
336336

337-
s = evalc( 'trprint(a)' );
337+
s = evalc( 'trprint2(a)' );
338338
tc.verifyTrue(isa(s, 'char') );
339339
tc.verifyEqual(size(s,1), 1);
340340

unit_test/TransformationsTest.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,18 @@ function trprint_test(tc)
957957
trprint(a, 'angvec', 'radian');
958958
trprint(a, 'angvec', 'radian', 'fmt', '%g');
959959
trprint(a, 'angvec', 'radian', 'fmt', '%g', 'label', 'bob');
960+
961+
% write to a file
962+
f = fopen('test.txt', 'w');
963+
trprint(a, 'fid', f);
964+
fclose(f);
965+
% read it back
966+
f = fopen('test.txt', 'r');
967+
s2 = fread(f, '*char');
968+
fclose(f);
969+
delete 'test.txt';
970+
tc.verifyTrue( all(s(:) == s2) );
971+
960972
end
961973

962974

0 commit comments

Comments
 (0)