SOFAtoolbox icon indicating copy to clipboard operation
SOFAtoolbox copied to clipboard

Error in writing and reading string variables

Open f-brinkmann opened this issue 1 year ago • 0 comments

There might be a bug in writing and reading string variables. The following code should write a string variabel of sice M=3 x S=5 with the three entries 'HD650' (first row), 'HD600' (second row), and 'HD490' (third row)

sofa = SOFAgetConventions('SimpleHeadphoneIR', 'a');

sofa = rmfield(sofa, 'MeasurementDate');
sofa.API.Dimensions = rmfield(sofa.API.Dimensions, 'MeasurementDate');

sofa = rmfield(sofa, 'SourceManufacturer');
sofa.API.Dimensions = rmfield(sofa.API.Dimensions, 'SourceManufacturer');

sofa = rmfield(sofa, 'ReceiverDescriptions');
sofa.API.Dimensions = rmfield(sofa.API.Dimensions, 'ReceiverDescriptions');

sofa = rmfield(sofa, 'EmitterDescriptions');
sofa.API.Dimensions = rmfield(sofa.API.Dimensions, 'EmitterDescriptions');

sofa.Data.IR = zeros(3, 2, 1024);
sofa.SourceModel = {'HD650'; 'HD600'; 'HD490'};   % <- this defines the string variable

SOFAsave('test_matlab.sofa', sofa);

In the sofa file, the dimensions of the variable are correct but the content is in the wrong order, as shown if openeing the file with HDFview:

Screenshot 2024-09-02 at 16 07 14

The data written in https://github.com/sofacoustics/SOFAtoolbox/blob/master/SOFAtoolbox/netcdf/NETCDFsave.m#L129-L132 as the variabel c. It has the correct size in line 129, so netcdf.putVar might behave differently than expected here.

The data is read in https://github.com/sofacoustics/SOFAtoolbox/blob/master/SOFAtoolbox/netcdf/NETCDFload.m#L135-L136 where you can find reshaping operations that scramble the letters back into the right order.

If this is a bug it means that all SOFA files written with SOFAtoolbox that have string-variables might have the error. This might not have been unnoticed until now, because the error is corrected when reading the data. So its fine for SOFAtoolbox but causes compatability issues with respect to other toolboxes.

f-brinkmann avatar Sep 02 '24 14:09 f-brinkmann