subscripted assignment dimension mismatch

1 view (last 30 days)
Kayleigh
Kayleigh on 13 Dec 2014
Answered: Guillaume on 13 Dec 2014
I keep experiencing an error - subscripted assignment dimension mismatch, within the matrix(chan,:)=eeg can anyone help?
chan=0;
while ~feof(fid); %loop to read lines into matrix
chan=chan+1;
tline = fgetl(fid);
disp(tline); %display the data to be put into the matrix to check it has been read
eeg=sscanf(tline,'%f');
matrix(chan,:)=eeg;
end

Answers (2)

Star Strider
Star Strider on 13 Dec 2014
I can’t be certain what the precise problem may be without seeing more of your code. If you preallocated ‘matrix’ to be a scalar or an empty matrix, that could potentially throw that error.
The usual ‘fix’ I suggest in such situations is to make a cell array out of ‘matrix’ to see if that solves the immediate problem:
matrix{chan} = eeg;
You can always go back to it later to parse your data.

Guillaume
Guillaume on 13 Dec 2014
For the line
matrix(chan, :) = eeg
to work, matrix must have as many columns as there are values in eeg. If that is not the case, then you'll get a subscripted assignment mismatch error. With your code, eeg has as many values as sscanf can decode from a line, which may or may not be the same number as the number of columns of matrix.
So, do you expect the same numbers of values on each line of your text file? If so, what do you want to happen when that is not the case? An error? Skip the line? Fill missing values with NaN and discard overflow? Something else?
If you don't expect the same number of values in each line, then you can't store them in a matrix. You could use a cell array instead.

Categories

Find more on EEG/MEG/ECoG in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!