Skip to content

Commit 73877a9

Browse files
committed
extractor for vector of strings + setters on Tag
1 parent 246de6c commit 73877a9

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

nix_mx.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,13 @@ void mexFunction(int nlhs,
171171
.reg("openFeature", GETBYSTR(nix::Feature, nix::Tag, getFeature))
172172
.reg("openSource", GETBYSTR(nix::Source, nix::Tag, getSource))
173173
.reg("openMetadataSection", GETCONTENT(nix::Section, nix::Tag, metadata))
174+
.reg("set_units", SETTER(const std::vector<std::string>&, nix::Tag, units))
175+
.reg("set_none_units", SETTER(const boost::none_t, nix::Tag, units))
174176
.reg("set_metadata", SETTER(const std::string&, nix::Tag, metadata))
175177
.reg("set_none_metadata", SETTER(const boost::none_t, nix::Tag, metadata))
178+
.reg("set_type", SETTER(const std::string&, nix::Tag, type))
179+
.reg("set_definition", SETTER(const std::string&, nix::Tag, definition))
180+
.reg("set_none_definition", SETTER(const boost::none_t, nix::Tag, definition))
176181
.reg("removeReference", REMOVER(nix::DataArray, nix::Tag, removeReference))
177182
.reg("removeSource", REMOVER(nix::Source, nix::Tag, removeSource))
178183
.reg("deleteFeature", REMOVER(nix::Feature, nix::Tag, deleteFeature));

src/utils/arguments.h

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ class extractor : public argument_helper<const mxArray> {
8888
template<typename T>
8989
std::vector<T> vec(size_t pos) const {
9090
std::vector<T> res;
91-
T *pr = mxGetPr(array[pos]);
91+
92+
T *pr;
93+
void *voidp = mxGetData(array[pos]);
94+
assert(sizeof(pr) == sizeof(voidp));
95+
memcpy(&pr, &voidp, sizeof(pr));
9296

9397
mwSize input_size = mxGetNumberOfElements(array[pos]);
9498
for (mwSize index = 0; index < input_size; index++) {
@@ -98,6 +102,35 @@ class extractor : public argument_helper<const mxArray> {
98102
return res;
99103
}
100104

105+
template<>
106+
std::vector<std::string> vec(size_t pos) const {
107+
/*
108+
To convert to a string vector we actually expect a cell
109+
array of strings. It's a logical representation since matlab
110+
arrays require that elements have equal length.
111+
*/
112+
std::vector<std::string> res;
113+
const mxArray *el_ptr;
114+
115+
mwSize length = mxGetNumberOfElements(array[pos]);
116+
mwIndex index;
117+
118+
for (index = 0; index < length; index++) {
119+
el_ptr = mxGetCell(array[pos], index);
120+
121+
if (!mxIsChar(el_ptr)) {
122+
throw std::invalid_argument("All elements of a cell array should be of a string type");
123+
}
124+
125+
char *tmp = mxArrayToString(el_ptr);
126+
std::string the_string(tmp);
127+
res.push_back(the_string);
128+
mxFree(tmp);
129+
}
130+
131+
return res;
132+
}
133+
101134
nix::NDSize ndsize(size_t pos) const {
102135
return mx_array_to_ndsize(array[pos]);
103136
}

tests/TestTag.m

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
funcs{end+1} = @test_open_metadata;
2020
funcs{end+1} = @test_retrieve_data;
2121
funcs{end+1} = @test_retrieve_feature_data;
22+
funcs{end+1} = @test_attrs;
2223
end
2324

2425
%% Test: Add sources by entity and id
@@ -289,3 +290,31 @@
289290
% TODO
290291
disp('Test Tag: retrieve feature ... TODO (proper testfile)');
291292
end
293+
294+
function [] = test_attrs( varargin )
295+
%% Test: Access Attributes
296+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
297+
b = f.createBlock('testBlock', 'nixBlock');
298+
t1 = b.create_tag('testTag', 'nixTag', [1, 2, 3, 4]);
299+
300+
assert(~isempty(t1.id));
301+
assert(strcmp(t1.name, 'testTag'));
302+
assert(strcmp(t1.type, 'nixTag'));
303+
304+
t1.type = 'nixTagTest';
305+
assert(strcmp(t1.type, 'nixTagTest'));
306+
307+
assert(isempty(t1.definition));
308+
t1.definition = 'definition';
309+
assert(strcmp(t1.definition, 'definition'));
310+
311+
t1.definition = '';
312+
assert(isempty(t1.definition));
313+
314+
assert(isempty(t1.units));
315+
t1.units = {'ms', 'mV'};
316+
assert(isequal(t1.units, {'ms', 'mV'}));
317+
318+
t1.units = {};
319+
assert(isempty(t1.units));
320+
end

0 commit comments

Comments
 (0)