Clear Filters
Clear Filters

Difficulty using 'readvars' function in a loop with different dimensions of data in the files

3 views (last 30 days)
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
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.

Sign in to comment.

Accepted Answer

Voss
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
151 154 157 150 159 146 158 150 144 149 150 161 150 159 149 150 159 165 144 160 159
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
Arrays have incompatible sizes for this operation.
(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
ans = 66
char('A'+1) % show the corresponding character result
ans = 'B'
char('filename'+'~') % add two character vectors (one is really a scalar - ok)
ans = 'äçêãìßëã'
char('filename'+'filename') % add two character vectors of the same size - ok
ans = 'ÌÒØÊÜÂÚÊ'
char('filename'+'10') % try to add incompatibly-sized character vectors - error
Arrays have incompatible sizes for this operation.
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
mack
mack on 8 Mar 2022
Thanks - this is indeed the source of the errror, and it can easily be solved by changing my single quotes for characters into double quotes for strings. I extremely appreciate your help!
Voss
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)

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!