Skip to content

Commit 9c50181

Browse files
authored
Fix compatibility issue in io.writeCompound for MATLAB R2019b and earlier (#693)
* Move some tests from tests.unit.FunctionTests to tests.unit.io.WriteTest * Add unittest (write non-scalar compound) * Make io.writeCompound backwards compatible
1 parent 576a33b commit 9c50181

File tree

3 files changed

+56
-25
lines changed

3 files changed

+56
-25
lines changed

+io/writeCompound.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ function writeCompound(fid, fullpath, data, varargin)
9292
isReferenceClass = strcmp(classes, 'types.untyped.ObjectView') |...
9393
strcmp(classes, 'types.untyped.RegionView');
9494

95+
if verLessThan('matlab', '9.8') % Matlab < 2020a
96+
% For MATLAB releases earlier than R2020a, character vectors must be
97+
% wrapped in a cell array, otherwise the write operation will fail with
98+
% the following error id "MATLAB:imagesci:hdf5dataset:badInputClass"
99+
% and message "The class of input data must be cellstring instead of char
100+
% when the HDF5 class is VARIABLE LENGTH H5T_STRING."
101+
for i = 1:length(names)
102+
val = data.(names{i});
103+
if ischar(val)
104+
data.(names{i}) = {data.(names{i})};
105+
end
106+
end
107+
end
108+
95109
% convert logical values
96110
boolNames = names(strcmp(classes, 'logical'));
97111
for iField = 1:length(boolNames)

+tests/+unit/+io/WriteTest.m

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,46 @@ function testWriteCompound(testCase)
8686
parsedData = table2struct( struct2table(parsedData) )';
8787
testCase.verifyEqual(data, parsedData);
8888
end
89+
90+
function testWriteCompoundMap(testCase)
91+
testCase.applyFixture(matlab.unittest.fixtures.WorkingFolderFixture)
92+
fid = H5F.create('test.h5');
93+
data = containers.Map({'a', 'b'}, 1:2);
94+
io.writeCompound(fid, '/map_data', data)
95+
H5F.close(fid);
96+
end
97+
98+
function testWriteCompoundEmpty(testCase)
99+
testCase.applyFixture(matlab.unittest.fixtures.WorkingFolderFixture)
100+
fid = H5F.create('test.h5');
101+
data = struct;
102+
testCase.verifyError(...
103+
@(varargin) io.writeCompound(fid, '/map_data', data), ...
104+
'MATLAB:imagesci:hdf5lib:libraryError')
105+
H5F.close(fid);
106+
end
107+
108+
function testWriteCompoundScalar(testCase)
109+
testCase.applyFixture(matlab.unittest.fixtures.WorkingFolderFixture)
110+
fid = H5F.create('test.h5');
111+
data = struct('a','b');
112+
io.writeCompound(fid, '/map_data', data)
113+
H5F.close(fid);
114+
end
115+
116+
function testWriteCompoundNonScalar(testCase)
117+
testCase.applyFixture(matlab.unittest.fixtures.WorkingFolderFixture)
118+
119+
numRows = 5;
120+
numericVector = rand(numRows, 1);
121+
charVector = char(randi([65 90], numRows, 1));
122+
%stringVector = string(char(randi([65 90], numRows, 1)));
123+
data = table(numericVector, charVector);
124+
125+
fid = H5F.create('test.h5');
126+
io.writeCompound(fid, '/map_data', data)
127+
H5F.close(fid);
128+
end
89129

90130
function testWriteCompoundOverWrite(testCase)
91131

@@ -140,4 +180,4 @@ function testWriteSoftLink(testCase)
140180
testCase.verifyTrue(strcmp(S.Links.Value{1}, targetPath))
141181
end
142182
end
143-
end
183+
end

+tests/+unit/FunctionTests.m

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,6 @@ function testString2ValidName(testCase)
1919
testCase.verifyEqual(string(validName), "at_id")
2020
end
2121

22-
function testWriteCompoundMap(testCase)
23-
testCase.applyFixture(matlab.unittest.fixtures.WorkingFolderFixture)
24-
fid = H5F.create('test.h5');
25-
data = containers.Map({'a', 'b'}, 1:2);
26-
io.writeCompound(fid, '/map_data', data)
27-
H5F.close(fid);
28-
end
29-
function testWriteCompoundEmpty(testCase)
30-
testCase.applyFixture(matlab.unittest.fixtures.WorkingFolderFixture)
31-
fid = H5F.create('test.h5');
32-
data = struct;
33-
testCase.verifyError(...
34-
@(varargin) io.writeCompound(fid, '/map_data', data), ...
35-
'MATLAB:imagesci:hdf5lib:libraryError')
36-
H5F.close(fid);
37-
end
38-
function testWriteCompoundScalar(testCase)
39-
testCase.applyFixture(matlab.unittest.fixtures.WorkingFolderFixture)
40-
fid = H5F.create('test.h5');
41-
data = struct('a','b');
42-
io.writeCompound(fid, '/map_data', data)
43-
H5F.close(fid);
44-
end
4522
function testIsNeurodatatype(testCase)
4623
timeSeries = types.core.TimeSeries();
4724
testCase.verifyTrue(matnwb.utility.isNeurodataType(timeSeries))
@@ -50,4 +27,4 @@ function testIsNeurodatatype(testCase)
5027
testCase.verifyFalse(matnwb.utility.isNeurodataType(dataPipe))
5128
end
5229
end
53-
end
30+
end

0 commit comments

Comments
 (0)