row by row matrix building

1 次查看(过去 30 天)
Colin Edgar
Colin Edgar 2015-11-6
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
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 个评论
Image Analyst
Image Analyst 2015-11-7
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by