Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.

1 view (last 30 days)
I'm trying to read a matrix from file, but my code works only if matrix is 3*3. So, if I make it smaller or bigger, this error occurs. I am new to MATLAB. Help me please to solve my problem
Here's my code:
fid = fopen ('Matrix_A.txt','r');
i = 1;
while ~feof(fid)
A(i, :) = str2num(fgets(fid));
i = i + 1;
end
fclose(fid);

Accepted Answer

KSSV
KSSV on 19 May 2020
fid = fopen ('Matrix_A.txt','r');
i = 1;
A = cell([],1) ;
while ~feof(fid)
A{i} = str2num(fgets(fid));
i = i + 1;
end
fclose(fid);
You can access A by using flower braces, A is a cell. A{1}, A{2}, .....etc.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!