Read .dat file in the format of a 3D matrix

5 Ansichten (letzte 30 Tage)
DM
DM am 12 Apr. 2021
Bearbeitet: Rupesh am 29 Feb. 2024
Hi,
I have a .dat file which is an output from IDL. This is a float 3D matrix with dimensions [145, 145, 249]. I tried to read it in Matlab as a 3D matrix unfortunately without success.
I tried the Import Tool app and many other solutions that I found online but none of them is working for my data:
I am using the following code to import a .dat file in IDL and it works fine.
%Define image dimensions and file name
Image = Mydata.dat
Nx = 145
Ny = 145
Nt = 249
%specify the .dat file path
Path = C:Users\
%create an empty array
Data = fltarr(Nx,Ny,Nt)
openr, 1, path + Image
readu, 1, data
close, 1
I would like to do something similar in Matlab. Any help would be greatly appreciated.
  2 Kommentare
KSSV
KSSV am 12 Apr. 2021
How is your .dat file? What is it's format?
DM
DM am 12 Apr. 2021
.dat file is basically a 3D image with floatting point values

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Rupesh
Rupesh am 16 Feb. 2024
Bearbeitet: Rupesh am 29 Feb. 2024
Hi DM,
After looking over your sample code, which you have provided, I realised that your objective is to read the .dat file data into 3D matrix output with desired dimensions. I am aware that this process may be a little challenging if you don't have access to the fltarr,openr, and readu functions in Matlab. However, you can still accomplish a comparable outcome by running the following commands in MATLAB script.
  • open the binary “.dat file for reading
  • read the floating-point data into MATLAB array.
  • Reshape the 1D array read from the file into a 3D matrix with correct dimensions.
  • Handle any potential issues such as file access errors or incorrect data interpretation due to endianness.
You can refer to below provided code snippet on how to read .Dat file and perform some operations on them.
%First portion of code to create a sample .dat file for testing with given
%specification
% Define image dimensions
Nx = 145;
Ny = 145;
Nt = 249;
% Generate random data
Data = rand(Nx, Ny, Nt, 'single');
% Specify the file path
Path = 'C:\checkoutput\'; % Make sure to use a path that is writable
Image = 'SampleMydata.dat';
FilePath = fullfile(Path, Image);
% Open the file for writing in binary mode
fileID = fopen(FilePath, 'wb');
% Check if the file was opened successfully
if fileID == -1
error('File could not be opened, check the name or path.');
end
% Write the data to the file in single precision
fwrite(fileID, Data, 'single');
% Close the file
fclose(fileID);
% Display a message
disp(['Sample .dat file created at: ' FilePath]);
Here the second portion contributes to reading the .dat file data into desired dimensions.
%Second Portion of code where file is created ,we can apply mathematical operations on the file
% to read the floating point data present in it
% Define image dimensions and file name
Nx = 145;
Ny = 145;
Nt = 249;
% Specify the .dat file path
Path = 'C:\checkoutput\'; % Make sure to use the correct path
Image = 'SampleMydata.dat';
FilePath = fullfile(Path, Image);
% Open the file for reading in binary mode
fileID = fopen(FilePath, 'rb');
% Check if the file was opened successfully
if fileID == -1
error('File could not be opened, check the name or path.');
end
% Read the data assuming it is of type 'single' (4 bytes per number)
% fread reads the data into a 1D array, so we will need to reshape it later
Data = fread(fileID, Nx*Ny*Nt, 'single');
% Close the file
fclose(fileID);
% Reshape the data to the correct dimensions
% The order of dimensions may vary depending on how the data is written
% Here, we assume that the first dimension changes fastest, then the second,
% and the third dimension changes slowest
Data = reshape(Data, [Nx, Ny, Nt]);
To know more about reading numbers from .dat file in the format of a 3d matrix, this MATLAB answers can be useful:
Hope it helps!

Community Treasure Hunt

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

Start Hunting!

Translated by