How to read multiple .bin files which are in one folder

조회 수: 8 (최근 30일)
C PRASAD
C PRASAD 2022년 9월 6일
답변: Arjun 2025년 1월 6일
i want to rad only .bin files and plot them using matlab
  댓글 수: 3
C PRASAD
C PRASAD 2022년 9월 7일
Thanks for the answer but it is not working for binary files
Stephen23
Stephen23 2022년 9월 7일
편집: Stephen23 2022년 9월 7일
"Thanks for the answer but it is not working for binary files"
Both approaches work for binary files, they will work for any files.
Of course you need to select an appropriate function for importing your binary file (it is not in the scope of a general help page to specify a function for every possible file format, that is your task).
Please upload two sample files by clicking the paperclip button.

댓글을 달려면 로그인하십시오.

답변 (1개)

Arjun
Arjun 2025년 1월 6일
I understand that you want to read multiple files from a folder which are ending with a .BIN extension.
You can refer to the following workflow for doing so:
  • Use the 'dir' function of MATLAB to list all the .Bin files present in the directory.
  • Iterate over the list of .BIN files and process them one by one.
  • Use 'fopen' to open the files.
  • Use 'fread' to read data content from the file.
  • Use 'fclose' to close the file after obtaining desired data.
Kindly refer to the following skelton code for reference:
% Extracting files ending with .bin extension
folderPath = 'path/to/your/folder';
filePattern = fullfile(folderPath, '*.bin');
binFiles = dir(filePattern);
% Iterate over the binary files one by one and process as needed
for k = 1:length(binFiles)
% Get the full file path
baseFileName = binFiles(k).name;
fullFileName = fullfile(folderPath, baseFileName);
% Open the file, opening file in reading mode
fileID = fopen(fullFileName, 'rb');
% Check if the file opened successfully
if fileID == -1
error('Cannot open file: %s', fullFileName);
end
% Read the data using fread. This will store data in column vector
% Read documentation of this function for other modes of reading.
data = fread(fileID);
% Close the file
fclose(fileID);
% Once the file has been read, you can plot the data as needed.
end
Please find attached documentation of functions used for reference:
I hope this will help!

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by