Difficulty using 'readvars' function in a loop with different dimensions of data in the files
Ältere Kommentare anzeigen
Hello, I am trying to initialize a cell array, by assigning each cell to the output from readvars. I have several different textfiles with a different number of lines, but they all have 5 columns.
My code goes something like this:
chunk = cell(1,13);
pulse = cell(1,13);
raw_tofs = cell(1,13);
for index = 1:1:13
[this_chunk, ~, this_pulse, ~, this_raw_tofs] = readvars('filename_dependent_on'+num2str(index));
chunk{index} = this_chunk;
pulse{index} = this_pulse;
raw_tofs{index} = this_raw_tofs;
end
or I have also tried changing inside the for loop to:
[ chunk{index}, ~, pulse{index}, ~, raw_tofs{index}] = readvars('filename_dependent_on'+num2str(index));
but both of these give a 'Matrix dimensions must agree' error. I've tried also clearing the contents of this_chunk, this_pulse, this_raw_tofs by setting them = [] before the readvars line.
I can't figure out how to make this work...
As well, to be clear, this loop works fine if I just loop any one file 13 times. It clearly has to do with the number of data points in each file being different, but I can't figure out a way to assign in a loop under that circumstance.
Any help is greatly appreciated! Thank you!
1 Kommentar
Max Alger-Meyer
am 7 Mär. 2022
It's sort of hard to debug without an example file, but one thing you might try is breaking the lines up into three separate function calls.
for index = 1:1:13
[this_chunk, ~, ~, ~, ~] = readvars('filename_dependent_on'+num2str(index));
[~, ~, this_pulse, ~, ~] = readvars('filename_dependent_on'+num2str(index));
[~, ~, ~, ~, this_raw_tofs] = readvars('filename_dependent_on'+num2str(index));
chunk{index} = this_chunk;
pulse{index} = this_pulse;
raw_tofs{index} = this_raw_tofs;
end
This is really only a guess as I can't see what's actually happening without a file to debug but my guess is that it doesn't like outputting instances where the vectors are of different length, and that the code only works for files in which there are the same number of rows.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!