Variable error on second file read when going through a sequence of files
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
Star Strider
2014년 9월 15일
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
Star Strider
2014년 9월 16일
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.
추가 답변 (1개)
Titus Edelhofer
2014년 9월 15일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!