Difficulty using 'readvars' function in a loop with different dimensions of data in the files
5 views (last 30 days)
Show older comments
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 Comment
Max Alger-Meyer
on 7 Mar 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.
Accepted Answer
Voss
on 8 Mar 2022
Edited: Voss
on 8 Mar 2022
I'm unable to reproduce the error with readvars itself, but I can generate that error on the same line as where readvars is used.
I realize that "'filename_dependent_on'+num2str(index)" is meant to be placeholder code representing the actual code you have there, but just in case you are using a similar syntax, namely, using plus (+) with character vector inputs, the error would happen when num2str(index) has more than one digit.
index = 1;
disp('filename_dependent_on'+num2str(index)) % no problem
index = 10;
try
disp('filename_dependent_on'+num2str(index)) % error happens
catch ME
disp(ME.message); % show the error message (and continue running code below)
end
(My MATLAB version gives the exact error message you report, "Matrix dimensions must agree.")
The reason is that plus treats character vectors like numeric vectors, so it's going add those character vectors together, not perform string concatenation, which may be what you intended with using plus (which does do string concatenation on strings but not on character vectors). Just like numeric arrays, when adding two character vectors, you can add a vector and a scalar ok, but adding two vectors requires that they have compatible dimensions.
'A'+1 % add a character vector 'A' and number 1
char('A'+1) % show the corresponding character result
char('filename'+'~') % add two character vectors (one is really a scalar - ok)
char('filename'+'filename') % add two character vectors of the same size - ok
char('filename'+'10') % try to add incompatibly-sized character vectors - error
My speculation is that as soon as index has more than one digit, then num2str(index) is non-scalar and you get the error here, in trying to construct the file name, before readvars is actually called. (This may be consistent with your observation that it works fine if you loop any one file 13 times - maybe index is not used in that case?)
If this is the cause of the error, you can fix it by constructing your file name using square brackets, e.g.,
['filename_dependent_on' num2str(index)]
If this is not what is causing the error, please upload some of the files readvars seems to have a problem with.
3 Comments
Voss
on 8 Mar 2022
@Mackenzie Peter-Fulford Van Rossem I'm glad it's working now!
And as @Stephen points out, you can avoid concatenation altogether regardless of whether you are using strings or character vectors, by using sprintf(), as in
sprintf('filename_dependent_on%d',index)
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!