Clear Filters
Clear Filters

Matrix dimensions must agree error

2 views (last 30 days)
Here is the code, and I suspect that the last batch of data is caused by last chunk remaining data. However, I am not sure how to handle it.
filename='rf.bin'; % filename='./rf.bin';
fid=fopen(filename,'r');
dataHeader=fread(fid,123,'uint8'); % skipping the header for .bin
NsperBatch = 1e3; % number of sample per batch
K=100; % Average every K set of values, K=100 in this case
magSpectrumMat=[];
while ~feof(fid)
magSpectrum=0;
for k=1:K
data=fread(fid,NsperBatch*2,'int16','b');
dataIQ=data(1:2:end)+1i*data(2:2:end);
dataSpectrum=fftshift(fft(dataIQ));
magSpectrum=magSpectrum+abs(dataSpectrum).^2;
end
magSpectrum = magSpectrum/K;
magSpectrumMat = [magSpectrumMat magSpectrum];
end
magSpectrumMat_dB=pow2db(magSpectrumMat);

Accepted Answer

Walter Roberson
Walter Roberson on 11 Oct 2017
You have
data=fread(fid,NsperBatch*2,'int16','b');
dataIQ=data(1:2:end)+1i*data(2:2:end);
The addition could fail if the fread() returns an odd number of values (but at least 3) instead of the expected even number of values. That could happen if the fread() encountered end of file.
If fread returned 1 value, then the addition would be of one value plus an empty vector, which would give an empty vector as a result, which is not an error. dataSpectrum would then involve the fft of an empty vector, which is not an error, giving an empty result. magSpectrum=magSpectrum+abs(dataSpectrum).^2; where the dataSpectrum is empty would give an empty right hand side, resulting in an empty result. Nothing would go wrong syntactically -- but it probably is not the result you would have preferred.
If fread returns empty, then the results are the same as just described.
fread can return empty because feof() does not always predict end of file -- that is, feof() does not always know that all of the data has been read and the next read will encounter end of file. In the context you use it for, feof() should predict end of file because of a non-standard extension MATLAB made to their I/O system, but it is safest not to rely on it.
I would recommend that you test the length() of data before using it. If you read in a partial block of K groups of data, then you will need to decide whether to ignore the entire group or to process what you got. If you decide to process what you got then you probably want to change the division by K in magSpectrum.
You could also consider vectorizing the loop:
data = fread(fid, [NsperBatch*2, K], 'int16', 'b'); %read down columns
dataIQ = complex(data(1:2:end,:), data(2:2:end,:));
dataSpectrum = fftshift(fft(dataIQ)); %performed down columns
magSpectrum = mean(abs(dataSpectrum).^2, 2);
  3 Comments
Ivy Chen
Ivy Chen on 12 Oct 2017
One more question, if I simply want to remove the last incomplete set of data, which has less than 1000 before put them into the matrix. How do I do that? thanks again.
Ivy Chen
Ivy Chen on 12 Oct 2017
I think you have address that in your answer to my other question. I will study that first and follow up in the other thread. thanks again.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!