From fafcfd487e02037944dde80d8cdcac8aee028f31 Mon Sep 17 00:00:00 2001 From: Ulf GRIESMANN Date: Sat, 5 Nov 2016 11:58:04 -0400 Subject: [PATCH] improved indexing --- Basic/@gds_element/private/get_element_data.c | 6 ++-- Basic/@gds_library/subsref.m | 35 ++++++++++++++++-- Basic/@gds_structure/subsref.m | 36 +++++++++++++++++-- Basic/gdsii_version.m | 2 +- ChangeLog | 9 +++++ 5 files changed, 80 insertions(+), 8 deletions(-) diff --git a/Basic/@gds_element/private/get_element_data.c b/Basic/@gds_element/private/get_element_data.c index 54f79d2..ee8c6e4 100644 --- a/Basic/@gds_element/private/get_element_data.c +++ b/Basic/@gds_element/private/get_element_data.c @@ -129,7 +129,7 @@ static mxArray* get_layer(element_t *pe) { if (pe->kind == GDS_SREF || pe->kind == GDS_AREF) - mexErrMsgTxt("get_element_data : element as no layer property"); + return empty_matrix(); else return mxCreateDoubleScalar((double)pe->layer); @@ -143,7 +143,7 @@ static mxArray* get_dtype(element_t *pe) { if (pe->kind == GDS_SREF || pe->kind == GDS_AREF) - mexErrMsgTxt("get_element_data : element as no [data|text|box|node] type property"); + return empty_matrix(); else return mxCreateDoubleScalar((double)pe->dtype); @@ -266,7 +266,7 @@ get_sname(element_t *pe) if (pe->kind == GDS_SREF || pe->kind == GDS_AREF) return mxCreateString(pe->sname); else - mexErrMsgTxt("get_element_data : element as no sname property"); + return empty_matrix(); return NULL; /* make compiler happy */ } diff --git a/Basic/@gds_library/subsref.m b/Basic/@gds_library/subsref.m index db8c511..849c7ae 100644 --- a/Basic/@gds_library/subsref.m +++ b/Basic/@gds_library/subsref.m @@ -10,11 +10,16 @@ % Ulf Griesmann, NIST, June 2011 - switch ins.type + % convert cs-lists --> cell arrays + itype = {ins.type}; + isubs = flatten({ins.subs}); + + % first indexing operator + switch itype{1} case '()' - idx = ins.subs{:}; + idx = isubs{1}; if ischar(idx) && idx == ':' gstrs = glib.st(1:end); @@ -39,5 +44,31 @@ error('gds_library.subsref : invalid indexing type.'); end + + % pass additional structure indexing to structure subsref method + if length(itype) > 1 + sins.type = itype{2}; + sins.subs = isubs{2}; + if iscell(gstrs) + gstrs = cellfun(@(x)subsref(x, sins), gstrs, 'Un',0); + else + gstrs = subsref(gstrs, sins); + end + end + + % flatten a cell array + function fca = flatten(ca) + + fca = cell(size(ca)); + + for k = 1:length(ca) + if iscell(ca{k}) + ct = ca{k}; + fca{k} = ct{1}; + else + fca(k) = ca(k); + end + end + end end diff --git a/Basic/@gds_structure/subsref.m b/Basic/@gds_structure/subsref.m index 8e79746..b047d1f 100644 --- a/Basic/@gds_structure/subsref.m +++ b/Basic/@gds_structure/subsref.m @@ -12,11 +12,17 @@ % Ulf Griesmann, NIST, June 2011 - switch ins.type + % convert cs-lists --> cell arrays + itype = {ins.type}; + isubs = flatten({ins.subs}); + + % first indexing operator + switch itype{1} case '()' - idx = ins.subs{:}; + idx = isubs{1}; + if ischar(idx) && idx == ':' gelp = gstruct.el(1:end); elseif length(idx) == 1 @@ -38,4 +44,30 @@ end + % pass additional element indexing to element subsref method + if length(itype) > 1 + eins.type = itype{2}; + eins.subs = isubs{2}; + if iscell(gelp) + gelp = cellfun(@(x)subsref(x, eins), gelp, 'Un',0); + else + gelp = subsref(gelp, eins); + end + end + + % flatten a cell array + function fca = flatten(ca) + + fca = cell(size(ca)); + + for k = 1:length(ca) + if iscell(ca{k}) + ct = ca{k}; + fca{k} = ct{1}; + else + fca(k) = ca(k); + end + end + end + end diff --git a/Basic/gdsii_version.m b/Basic/gdsii_version.m index ec9dc76..4788bc1 100644 --- a/Basic/gdsii_version.m +++ b/Basic/gdsii_version.m @@ -17,7 +17,7 @@ % toolbox version and version date tb_version = '136'; - tb_date = '2016-October-24'; + tb_date = '2016-November-04'; ltb_ver = [tb_version, ' (', tb_date, ')']; if exist('OCTAVE_VERSION') interpreter = 'Octave'; diff --git a/ChangeLog b/ChangeLog index 437b51f..6258f11 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2016-November-05 +- version 137 +- return [] in 'get_element_data' if an element does not have a requested + property instead of throwing an error. +- improved 'subsref' methods for gds_library and gds_structure objects that + enable more complex indexing operations. E.g. it is now possible to + write S(10).xy to get the xy record of the 10th element in a + structure without having to copy the element first. + 2016-October-24 - version 136 - finally squashed bug in gdsio::now