How to clear existing array size? I am getting "Subscripted assignment dimension mismatch" Error.

I have multiple binary files to read and trying to save the data into a cell. I tried reading data using the the code given below. But it shows error "Subscripted assignment dimension mismatch" after reading few files. I figured it is showing error due to different array size. So can you please tell me is there any way to clear arrays so that new array of different dimension can be saved in the cell.

2 comentarios

The best way I've found to do this is by clearing the entire variable using clear(variable). Alternatively, if you're looking to replace a cell value, just turn the value into a blank with {}.
Do NOT use eval for trivial code like this:
files = dir('*.txt');
for i=1:length(files)
eval(['load ' files(i).name ' -ascii']);
end
It is much neater, more efficient, and easier to debug if you just call the function directly:
S = dir('*.txt');
for k = 1:numel(S)
S(k).data = load(S(k).name,'-ascii');
end

Iniciar sesión para comentar.

Respuestas (1)

Stephen23
Stephen23 el 15 de Nov. de 2018
Editada: Stephen23 el 15 de Nov. de 2018
clear is rarely needed in well written code. It is usually simpler and more efficient to just let MATLAB manage the memory by correctly defining/preallocating variables. In your code, the problem presumably happens on this line (you forgot to give us the complete error message, so I had to guess where the error occurs):
X(:,k)= fread(fid,1,'float');
where you use indexing to allocate values to X, but X was not defined anywhere earlier in the code (so it can grow, but never shrink... ouch!). The simple soluiton is to preallocate X with its final size before the loops: you already know how many loop iterations there will be, so try something like this (not working, just to get you started):
X = nan(1,a(4)); % preallocate!
for k=1:a(4)
X(:,k)= fread(fid,1,'float'); %%reading data and saving in X
end
The correct solution depends on the size/s of the data returned by fscanf, which you did not tell us anything about. In any case, it will be more efficient than calling clear before each loop.
To be honest that code does not look very efficient: by the time you get to fread you have four nested loops. The file you are importing seems to be a text file, so why not just use one of the inbuilt file-importing tools? If you upload your file we can help you with this.

1 comentario

Thank you for your reply. I defined X as you said. Also I did not use eval to read the files I used the text file with the names of input to open.
Still I am getting the same error.
ERROR::
Subscripted assignment dimension mismatch.
Error in test2 (line 50)
dat(j,:)=fftshift(X);
I have uplaoded two of the input files I am using on this drive folder.
I hope you can help!

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 15 de Nov. de 2018

Comentada:

el 16 de Nov. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by