Quick question here I hope. Am modifying some code, can't see why this won't work:
j=1
while ischar (tline);
%define indexes, below as idx, etc etc
tline = fgetl(fid); %reads the next line in input file
dt = textscan(tline,'%s','delimiter',',');
tm = datenum(dt{1}(didx));
d1 = str2double(dt{1}(1idx));
d2 = str2double(dt{1}(2idx));
d3 = str2double(dt{1}(3idx));
mat(j,:) = [tm d1 d2 d3]; ;
j = j+1;
tline = fgetl(fid);
end;
The stumbling block is the mat(j,:) portion. Error message says 'Subscripted assignment dimension mismatch.' Tried different ways of creating mat[] prior to this loop and in different ways but no luck. Could do a whole different approach, but don't want to build a whole new routine.

댓글 수: 1

Image Analyst
Image Analyst 2015년 11월 7일
1idx, 2idx, and 3idx are not valid variable names.

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2015년 11월 7일

0 개 추천

What is the size of mat? Does it have 4 columns? What is the value of tline and j when the error is thrown? Maybe try preallocating mat before the while loop
mat = zeros(1, 4);

댓글 수: 5

The example data being tested is 40449 x 7, actual size varies. So 7 columns (posted code has 4). When error is thrown, j = 1 and tline is the first line of data. I tried preallocating mat in different ways mat[], mat = zeros(x,y) etc. Is the indexing approach correct:
mat(j,:) = [tm d1 d2 d3 etc]; ?
Image Analyst
Image Analyst 2015년 11월 7일
Yes, as long as there are the same number of elements on the right hand side as there are columns in the matrix. Can you finish answering my questions, like what is tline?
Here's the rest of the code:
flnm = 'C:\testfile.csv'
fid = fopen(flnm);
j = 1;
tline = fgetl(fid); %reads past top header lines
tline = fgetl(fid); %reads past top header lines
mat = zeros(40449,7);
while ischar(tline)
.....then what I posted initially
tline='2015-05-16 12:54:00,65433,0,1,402.2262,3.598117,99.87829,-5.04303,1.07'
It will sometimes go through j = 1 and then first row of mat is populated correctly. Then it bombs on j = 2. error**Improper assignment with rectangular empty matrix.
Well you can't assign 4 numbers to an entire row of 7 columns. If you just want the first 4 columns assigned, you can index it like this:
mat(j,1:4) = [tm d1 d2 d3];
The final 3 columns (columns 5, 6, and 7) will be untouched and remain as zero.
Colin Edgar
Colin Edgar 2015년 11월 9일
I realize that. Sorry my example is smaller than my actual testfile. I still haven't solved this but am doing a workaround. Still interested in a solution. Thx

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2015년 11월 6일

댓글:

2015년 11월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by