Skip to content

Commit

Permalink
Merge commit 'ceab024e9a286d6ac7c102e2a6d6e28d047dd3c6'
Browse files Browse the repository at this point in the history
  • Loading branch information
ezander committed Jun 15, 2018
2 parents ba11d40 + ef66a71 commit 39b16fd
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 17 deletions.
5 changes: 3 additions & 2 deletions plotting/spy2.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ function spy2(A, varargin)
[lin_range,options]=get_option( options, 'ling_range', 'auto' );
check_unsupported_options( options, mfilename );

% get index of nonzero elements
% get index of nonzero elements (note that first dimension in a matrix is
% vertical, so swap x and y)
cmp = max(min_abs_val, max(abs(A(:)))*min_rel_val);
[x,y]=find(abs(A)>cmp);
[y,x]=find(abs(A)>cmp);

% make sure x and y are row vectors (not true if dim(A,1)==1)
x=reshape(x,1,[]); y=reshape(y,1,[]);
Expand Down
33 changes: 24 additions & 9 deletions util/disp_func.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function str=disp_func( func )
function str=disp_func( func, varargin )
% DISP_FUNC Convert function handle to string description.
% STR=DISP_FUNC( FUNC ) returns the string representation of FUNC.
% Partially supplied arguments are put at the appropriate places.
Expand Down Expand Up @@ -26,11 +26,16 @@

check_num_args(nargin, 1, inf, mfilename);

options = varargin2options(varargin, mfilename);
[indent,options]=get_option(options, 'indent', false);
%[separgs,options]=get_option(options,'separgs', false);
check_unsupported_options(options);

if isempty(func)
str='<none>';
else
[handle,args]=collect_args( func );
str=sprintf('%s(%s)', handle2str( handle ), args2str(args));
str=sprintf('%s(%s)', handle2str( handle ), args2str(args, indent));
end

if nargout==0
Expand All @@ -46,7 +51,7 @@
elseif ischar(handle)
s=handle;
else
error( 'unknown' );
error( 'sglib:unknown_func_type', 'Unknown function type' );
end

function [handle,args]=collect_args( func )
Expand All @@ -73,7 +78,7 @@
out=varargin;


function s=args2str( args )
function s=args2str( args, indent )
% ARGS2STR Convert arguments to string representation
last={};

Expand All @@ -87,26 +92,36 @@
args(end)=[];
end

if indent
sep = sprintf(',\n ');
else
sep = ', ';
end

s='';
for i=1:length(args)
if is_pos_arg(args{i})
s=[s strvarexpand( ', <arg$args{i}{1}$>' )]; %#ok<AGROW>
s=[s sep strvarexpand( '<arg$args{i}{1}$>', 'quotes', true )]; %#ok<AGROW>
else
s=[s strvarexpand( ', $args{i}$' )]; %#ok<AGROW>
s=[s sep strvarexpand( '$args{i}$', 'quotes', true )]; %#ok<AGROW>
end
end

% If there are trailing arguments, put an ellipses in between
if ~isempty( last )
s=[s ', ...'];
for i=1:length(last)
s=[s strvarexpand( ', $last{i}$' )]; %#ok<AGROW>
s=[s sep strvarexpand( '$last{i}$', 'quotes', true )]; %#ok<AGROW>
end
end

% Cut the ', ' from the beginning of the string
if length(s)>=2
s=s(3:end);
if length(s)>=length(sep)
if indent % only cut the comma, not the whole separator
s=s(2:end);
else
s=s(length(sep)+1:end);
end
end


Expand Down
16 changes: 10 additions & 6 deletions util/strvarexpand.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
[maxarr, options]=get_option(options, 'maxarr', 10);
[maxcell, options]=get_option(options, 'maxcell', 50);
[show_expr, options]=get_option(options, 'show_expr', false);
[quotes, options]=get_option(options, 'quotes', false);
check_unsupported_options(options, mfilename);

exstr='';
Expand All @@ -46,7 +47,7 @@
catch
part=['<err:' orig '>'];
end
part=tostring( part, orig, maxarr, maxcell );
part=tostring( part, orig, maxarr, maxcell, quotes );
if show_expr
part=[orig ':' part];
end
Expand All @@ -66,7 +67,7 @@
stop_check;
end

function str=tostring( val, orig, maxarr, maxcell )
function str=tostring( val, orig, maxarr, maxcell, quotes )
if islogical(val)
val=double(val);
end
Expand All @@ -83,7 +84,7 @@
if i>1
str=[str, ', ']; %#ok<*AGROW>
end
str=[str, tostring(val(i),sprintf('%s[%d]',orig,i), maxarr, maxcell)];
str=[str, tostring(val(i),sprintf('%s[%d]',orig,i), maxarr, maxcell, quotes)];
end
if maxarr<numel(val)
str=[str, ', ...']; %#ok<*AGROW>
Expand All @@ -92,6 +93,9 @@
end
elseif ischar(val)
str=reshape( val, 1, []);
if quotes
str = ['''' str ''''];
end
elseif isa(val, 'function_handle')
str=['@', func2str(val)];
elseif iscell(val)
Expand All @@ -100,7 +104,7 @@
if i>1
str=[str, ', '];
end
str=[str, tostring(val{i},sprintf('%s{%d}',orig,i), maxarr, maxcell)];
str=[str, tostring(val{i},sprintf('%s{%d}',orig,i), maxarr, maxcell, quotes)];
end
if maxcell<numel(val)
str=[str, ', ...']; %#ok<*AGROW>
Expand All @@ -114,10 +118,10 @@
if i>1
str=[str, ', '];
end
str=[str, name, '=', tostring(val.(name),sprintf('%s.%s',orig,name), maxarr, maxcell)];
str=[str, name, '=', tostring(val.(name),sprintf('%s.%s',orig,name), maxarr, maxcell, quotes)];
end
str=[str, ')'];
elseif isa(val, 'SglibObject')
elseif isa(val, 'SglibObject') || isa(val, 'SglibHandleObject')
str=val.tostring();
else
warning('strvarexpand:type', 'Type of $%s$ not supported: %s', orig, class(val) );
Expand Down
36 changes: 36 additions & 0 deletions util/unittest_disp_func.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function unittest_disp_func(varargin)
% UNITTEST_DISP_FUNC Test the DISP_FUNC function.
%
% Example (<a href="matlab:run_example unittest_disp_func">run</a>)
% unittest_disp_func
%
% See also DISP_FUNC, MUNIT_RUN_TESTSUITE

% Elmar Zander
% Copyright 2017, Inst. of Scientific Computing
%
% This program is free software: you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by the
% Free Software Foundation, either version 3 of the License, or (at your
% option) any later version.
% See the GNU General Public License for more details. You should have
% received a copy of the GNU General Public License along with this
% program. If not, see <http://www.gnu.org/licenses/>.

munit_set_function( 'disp_func' );

fun1 = funcreate(@testtest, @funarg, 'foo', @funarg, 'bar', @ellipsis, 'baz');
assert_equals(disp_func(fun1), ...
'@testtest(<arg1>, ''foo'', <arg2>, ''bar'', ..., ''baz'')');

fun2 = funcreate('foobar', @funarg, 'foo');
assert_equals(disp_func(fun2), ...
'foobar(<arg1>, ''foo'')');

fun2 = funcreate('foobar', @funarg, 'foo');
assert_equals(disp_func(fun2, 'indent', true), ...
sprintf('foobar(\n <arg1>,\n ''foo'')'));

fun3 = funcreate(123, @funarg, 'foo');
assert_error(funcreate(@disp_func, fun3), 'sglib:unknown_func_type');

4 changes: 4 additions & 0 deletions util/unittest_strvarexpand.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@
assert_equals( strvarexpand('$[1,2,3,4,5]$', 'maxarr', 3), '[1, 2, 3, ...]', 'maxarr3' );
assert_equals( strvarexpand('${1,2,3,4,5}$', 'maxcell', 2), '{1, 2, ...}', 'maxcell2' );
assert_equals( strvarexpand('${1,2,3,4,5}$', 'maxcell', 4), '{1, 2, 3, 4, ...}', 'maxcell4' );

% Test with quote
assert_equals( strvarexpand('$test$', 'quotes', false), '1234', 'noquotes' );
assert_equals( strvarexpand('$test$', 'quotes', true), '''1234''', 'quotes' );

0 comments on commit 39b16fd

Please sign in to comment.