anyone know how to open .dmi file

10 vues (au cours des 30 derniers jours)
mohd akmal masud
mohd akmal masud le 8 Oct 2024
Modifié(e) : Sameer le 8 Oct 2024
Dear all,
I have .dmi file (as attached). Then I try coding as below:
unzip alders.zip
fid = fopen('alders.dmi','rb');
arr = fread(fid,'int8');
fclose(fid);
dim = [64,64,128];
arr = reshape(arr,dim)
But I got error
Error using reshape
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension.
Anyone can help me?

Réponses (3)

Shubham
Shubham le 8 Oct 2024
Modifié(e) : Shubham le 8 Oct 2024
It appears that the error is due to a mismatch between the number of elements in your array and the dimensions you’re trying to reshape it into. The reshape function requires that the total number of elements remains the same before and after reshaping.
unzip alders.zip
fid = fopen('alders.dmi','rb');
arr = fread(fid,'int8');
% Actual Size of the array
disp(size(arr));
524289 1
% Number of elements in reshaped array
disp(64*64*128);
524288
% Notice that the number of elements are not same
fclose(fid);
dim = [64,64,128];
% The number of elements should be same for both cases
% Removing the first element of the array.
arr = arr(2:length(arr));
arr = reshape(arr,dim); %No errors now
In order to resolve the error you are facing, either make changes to the dimensions for the reshaped array, or truncate one element to keep the same dimensions. This is to ensure that the number of elements should remain constant after reshaping the array.
I hope this resolves the issue!

Les Beckham
Les Beckham le 8 Oct 2024
There are two issues. One is that your fread command is converting the int8 data to doubles when reading. The other is that the number of bytes in the .dmi file is not equal to 64 * 64 * 128.
unzip alders.zip
fid = fopen('alders.dmi','rb');
arr = fread(fid,'*int8'); %<<< add the * to tell Matlab not to convert the numbers to doubles
fclose(fid);
dim = [64,64,128];
size(arr,1)
ans = 524289
prod(dim)
ans = 524288
size(arr,1) == prod(dim)
ans = logical
0
% arr = reshape(arr,dim)

Sameer
Sameer le 8 Oct 2024
Modifié(e) : Sameer le 8 Oct 2024
The error occured because the total number of elements is not equal to 64 * 64 * 128.
You can verify and adjust as follows:
numElements = numel(arr);
dim1 = 64;
dim2 = 64;
% Calculate the third dimension
dim3 = numElements / (dim1 * dim2);
% Check if dim3 is an integer
if mod(dim3, 1) ~= 0
error('The dimensions do not match the number of elements.');
end
% Reshape the array
arr = reshape(arr, [dim1, dim2, dim3]);
disp(['Reshaped array size: ', num2str(size(arr))]);
Hope this helps!

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by