Resolve readChannelGroups out of Bounds when combining multiple .tdms files

1 view (last 30 days)
I have multiple tdms files which I am trying to combine into one using the information available at https://uk.mathworks.com/help/daq/example-merge-multiple-tdms-files.html
When I use this code I get the following error:
readChannelGroups out of Bounds. startIndex(0) + readSize(52417) <= max(numSamplesAcrossChannelGroups)(52276)
From this I gather that there is a limit on the number of rows that can be read, which has been set to 52276. This is the number of rows in the first file in the DataStore. The code runs until it meets a file larger than the first, with 52417 rows.
I tried to remedy this by setting tdmsDatastore ReadSize to 51000 but then I get this error:
readChannelGroups out of Bounds. startIndex(51000) + readSize(1417) <= max(numSamplesAcrossChannelGroups)(52276)
Is there a way I can combine multiple tdms files into one tdms file where it doesn't matter if the files are different lengths?
Here is my full code. If anyone has a suggestion as to how to share the tdms data (or minimal data) in a sensible way then it is welcome!
foldername = uigetdir();
% move up one folder by removing the last bit of the foldername and
% assigning that to variable called "f"
f = foldername(1:end-10);
cd(f);
id = foldername(end-8:end); % new filename = final 9 chars of folder name
newfilename = [id '.tdms'];
ds = tdmsDatastore(foldername, ReadSize = "file");
data = readall(ds);
tdmswrite(newfilename, data);
while(hasdata(ds))
data = read(ds); %read one file.
tdmswrite(newfilename, data);
end
clear

Accepted Answer

Aylin
Aylin on 7 Feb 2023
Hello Eleanor, this is likely to be a bug in tdmsDatastore. I have forwarded this to the tdmsDatastore developers.
As a workaround, can you replace this loop:
ds = tdmsDatastore(foldername, ReadSize="file")
while(hasdata(ds))
data = read(ds)
tdmswrite(newfilename, data);
end
With something like this?
fileSet = matlab.io.datastore.FileSet(foldername, FileExtensions=".tdms");
while(fileSet.hasNextFile())
data = tdmsread(fileSet.nextfile().Filename)
tdmswrite(newfilename, data);
end
I hope this helps!
Rylan
  1 Comment
Eleanor Hassan
Eleanor Hassan on 7 Feb 2023
Thank you so much Rylan! This has completely resolved my problem.
For anyone looking in the future, here is the entirety of the working code:
foldername = uigetdir();
f = foldername(1:end-10);
cd(f);
id = foldername(end-8:end)
newfilename = [id '.tdms'];
fileSet = matlab.io.datastore.FileSet(foldername, FileExtensions=".tdms");
while(fileSet.hasNextFile())
data = tdmsread(fileSet.nextfile().Filename)
tdmswrite(newfilename, data);
end
clear

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!