Skip to content

Commit 634682b

Browse files
committed
[c++/m] Add DataArray dataExtent handling
- Changes matlab dynamic attribute "shape" to the original designation "dataExtent". - Adds the setDataExtent method on the c++ and the matlab side and introduces a corresponding test.
1 parent 7925f6c commit 634682b

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
lines changed

+nix/DataArray.m

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
nix.Dynamic.add_dyn_attr(obj, 'unit', 'rw');
3030
nix.Dynamic.add_dyn_attr(obj, 'expansionOrigin', 'rw');
3131
nix.Dynamic.add_dyn_attr(obj, 'polynomCoefficients', 'rw');
32-
nix.Dynamic.add_dyn_attr(obj, 'shape', 'rw');
32+
nix.Dynamic.add_dyn_attr(obj, 'dataExtent', 'rw');
3333
end;
3434

3535
% -----------------
@@ -129,6 +129,20 @@ function write_all(obj, data)
129129
function s = datatype(obj)
130130
s = nix_mx('DataArray::dataType', obj.nix_handle);
131131
end
132-
132+
133+
% set data extent enabels to increase the original size
134+
% of a data array within the same dimensions.
135+
% e.g. increase the size of a 2D array [5 10] to another
136+
% 2D array [5 11]. Changing the dimensions is not possible
137+
% e.g. changing from a 2D array to a 3D array.
138+
% Furthermore if the extent shrinks the size of an array
139+
% or remodels the size of an array to a completely different
140+
% shape, existing data that does not fit into the new shape
141+
% will be lost!
142+
function [] = set_data_extent(obj, extent)
143+
nix_mx('DataArray::setDataExtent', obj.nix_handle, extent);
144+
% update changed dataExtent in obj.info
145+
obj.info = nix_mx(strcat(obj.alias, '::describe'), obj.nix_handle);
146+
end
133147
end;
134148
end

nix_mx.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ void mexFunction(int nlhs,
234234
methods->add("DataArray::dimensionCount", nixdataarray::dimensionCount);
235235
methods->add("DataArray::setPolynomCoefficients", nixdataarray::polynomCoefficients);
236236
methods->add("DataArray::dataType", nixdataarray::dataType);
237+
methods->add("DataArray::setDataExtent", nixdataarray::setDataExtent);
237238

238239
classdef<nix::Source>("Source", methods)
239240
.desc(&nixsource::describe)

src/nixdataarray.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace nixdataarray {
2121

2222
mxArray *describe(const nix::DataArray &da) {
2323
struct_builder sb({ 1 }, { "id", "type", "name", "definition", "label",
24-
"shape", "unit", "expansionOrigin", "polynomCoefficients" });
24+
"dataExtent", "unit", "expansionOrigin", "polynomCoefficients" });
2525

2626
sb.set(da.id());
2727
sb.set(da.type());
@@ -130,4 +130,10 @@ namespace nixdataarray {
130130
output.set(0, string_nix2mex(da.dataType()));
131131
}
132132

133+
void setDataExtent(const extractor &input, infusor &output) {
134+
nix::DataArray da = input.entity<nix::DataArray>(1);
135+
nix::NDSize size = input.ndsize(2);
136+
da.dataExtent(size);
137+
}
138+
133139
} // namespace nixdataarray

src/nixdataarray.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ namespace nixdataarray {
3939

4040
void dataType(const extractor &input, infusor &output);
4141

42+
void setDataExtent(const extractor &input, infusor &output);
43+
4244
} // namespace nixdataarray
4345

4446
#endif

tests/TestDataArray.m

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
funcs{end+1} = @test_dimensions;
3030
funcs{end+1} = @test_dimension_count;
3131
funcs{end+1} = @test_datatype;
32+
funcs{end+1} = @test_set_data_extent;
3233
end
3334

3435
function [] = test_attrs( varargin )
@@ -484,11 +485,11 @@
484485

485486
daAliasWa = f.blocks{1}.create_data_array_from_data('aliasDimWATest2', ...
486487
'nix.DataArray', [1; 2; 3]);
487-
assert(isequal(daAliasWa.shape, [3 1]));
488+
assert(isequal(daAliasWa.dataExtent, [3 1]));
488489

489490
daAliasWa = f.blocks{1}.create_data_array_from_data('aliasDimWATest3', ...
490491
'nix.DataArray', [1 2 3; 2 4 5; 3 6 7]);
491-
assert(isequal(daAliasWa.shape, [3 3]));
492+
assert(isequal(daAliasWa.dataExtent, [3 3]));
492493
end
493494

494495
%% Test: Dimension count
@@ -530,3 +531,15 @@
530531
da.write_all(logical([1 0 1 1 1]));
531532
assert(strcmp(da.datatype, 'logical'));
532533
end
534+
535+
%% Test: Set extent
536+
function [] = test_set_data_extent( varargin )
537+
fileName = fullfile(pwd, 'tests', 'testRW.h5');
538+
f = nix.File(fileName, nix.FileMode.Overwrite);
539+
b = f.create_block('testBlock', 'nixblock');
540+
541+
da = b.create_data_array_from_data('testDataArray1', 'nix.DataArray', [1 2 3; 4 5 6]);
542+
extent = [4 6];
543+
da.set_data_extent(extent);
544+
assert(da.dataExtent(1) == extent(1) && size(da.read_all, 2) == extent(2));
545+
end

0 commit comments

Comments
 (0)