Variable error on second file read when going through a sequence of files
1 view (last 30 days)
Show older comments
Hi.
I have a working code for a single file which extracts data from a csv file, performs some calculations and then exports as a .txt file. I have another code where it runs through all the files in a directory so I have amended that bit of code to this new code. However, it performs fine for the first file in the directory and then gives me an error of:
Index exceeds matrix dimensions.
at one of my variables. There must be something wrong on the next loaded file or it's being messed up by the variables from the first file. If I comment out the variable it progresses until another line of code, one of the calculations and gives a different error of:
Non-finite endpoints or increment for colon operator in index
My code is something like this
clear;
clc;
Files = transpose( dir( '*.csv' ) );
for file = Files
disp( file.name )
FileName1 = (file.name)
[num text raw] = xlsread(FileName1);
% Variables are along these lines etc
detection = num(5,2);
Voltage = num(12,5);
date = text(14,3);
myCode;
output to .txt file;
end
The error first appears at the variable:
date = text(14,3);
on the second file in the sequence. If I comment out that variable it runs until one of the equations later in the code. I really don't know what's going on, any ideas?
0 Comments
Accepted Answer
Star Strider
on 15 Sep 2014
I’m surprised any of your index references work at all. MATLAB does not automatically decrement indices unless you tell it to. If you don’t tell it specifically, it returns an empty matrix:
x = 10:20;
yi = x(5:2) % Implied Increment
yd = x(5:-1:2) % Declared Decrement
Also, I’m curious as to your use of transpose here:
Files = transpose( dir( '*.csv' ) );
The reason for that eludes me.
7 Comments
Star Strider
on 16 Sep 2014
My pleasure!
You probably need to import the errant file manually, then save it and its appropriate variable fields as a .mat file.
I would do that with all of them. Then you simply load them as necessary, and not worry about reading the spreadsheet files each time.
More Answers (1)
Titus Edelhofer
on 15 Sep 2014
Hi,
some comments: first of all your indexing is wrong: 5:2 is the empty set. Try 2:5 instead (same of course for Voltage, date):
Second: do all files have the same input? If you get an index error, typically this is due to files having different sizes (and therefore num, text, raw having different sizes).
Titus
See Also
Categories
Find more on File Operations 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!