How can I convert a file that contains ASCII formatted letters into normal text using Matlab?

11 visualizaciones (últimos 30 días)
This is how the file looks like in .txt file ( its just part of it) It can be any of these formats: binary double (double-precision floating point), binary single (single-precision floating point), or binary int32 (signed 32 bit integers), I don't know which one it is, and these commands can be used to help execute the function.
  • fopen, fclose, fread, fwrite, fseek
  • char, fprintf, disp, load, circshift, length, find, strfind, cell
  • if, while, for, switch
Any help will be much appreciated!
Screenshot 2020-01-14 at 20.28.37.png
  3 comentarios
Andrew Janke
Andrew Janke el 31 de En. de 2020
Based on your comments and the screenshot, that looks like a binary file, not a text file.
Walter Roberson
Walter Roberson el 31 de En. de 2020
Editada: Walter Roberson el 31 de En. de 2020
Is this a one time question about the representation of a particular file, or is this a homework assignment to write a program that needs to be able to decide which of those three formats a given input file is? Your list of permitted commands make this sound like homework.

Iniciar sesión para comentar.

Respuestas (1)

Cris LaPierre
Cris LaPierre el 31 de En. de 2020
Editada: Cris LaPierre el 31 de En. de 2020
Agreed. Looks like binary data. You can still convert it to text, but you have to know something about the file format.
In the simplest case, everythign is encoded the same and you can use a single fread command. As an example, I have the raw data from an MRI scan. The values are all encoded in float32 and consist of a real value and an imaginary value. I have to know that, and know what the orignal matrix size was. With that info, I can load the binary data into MATLAB as a numeric matrix.
Here's what that code would look like
function mm = mribin_unpack(filename,ylen,xlen)
fid = fopen(filename,'r','b');
mridata=fread(fid,[2,inf],'float32');
mridata=complex(mridata(1,:),mridata(2,:));
mm=reshape(mridata,ylen,xlen);
fclose(fid);
end
In more complex file formats, the number of bytes used to encode each entry can vary value to value. In this sort of setup, the size is typically encoded into the file. For example, you read a byte that tells you how many bytes are used to store the next value. this can be more complicated to figure out on your own, so I typically use a hex editor to help inspect the raw file to help me figure it out. I've used the Hackman Hex Editor in the past. It's free.
A script to read these files can be quite long, especially if you are trying to reverse engineer the format. This is a snippet of code I wrote for such a file.
%% open file for reading
fid=fopen(filename,'r','l');
ver_nm = fread(fid,8,'*char')';
%% Read in next section
while ~feof(fid)
section = upper(fread(fid,4,'*char')');
switch section
case 'DATA'
flag = logical(fread(fid,1,'int'));
if flag
bytes = fread(fid,1,'int');
% div 4 because 4 bytes per float
% div 2 because 2 rows x indicated size
DATA=fread(fid,[2,bytes/4/2],'float32');
DATA=complex(DATA(1,:),DATA(2,:));
end
...
The actual code is over 1200 lines long, so I've trimmed it enough to give you an idea.
Hope this helps.

Categorías

Más información sobre Low-Level File I/O en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by