IDL readu equivalent in matlab

2 views (last 30 days)
OldCar
OldCar on 11 Mar 2016
Commented: OldCar on 17 Mar 2016
Hello, I have a .pro script (IDL) and I want to implement in Matlab. I don't know the equivalent of READU (read unformatted file) of IDL in Matlab. The original script was:
OPENR, lun, fileName2, /GET_LUN ; read the input file
var0a = BYTARR(4) ; create array to load 4 char source program identifier
var0b = var0a; ; create 2nd array to load 4 char file type identifier
var1 = FLTARR(1)
var2 = LONARR(2)
var3 = DBLARR(4)
READU, lun, var0a, var1, var0b, var2, var3
If I try to open the file with notepad I see only symbols.
I have used
fid = fopen('GSS1_t001_cx_ImgRall.dat');
col = fread(fid);
fclose(fid);
and I get a column vector, but I have to assign as made in IDL, so I would like to get an array of 4 char, an array of one element, an array of 4 char, an array of two elements, an array of four elements.
I don't know the use of lun. Maybe it tells us the number of times that we have to get the results.

Accepted Answer

Walter Roberson
Walter Roberson on 11 Mar 2016
IDL's lun stands for "logical unit number", and it serves the same purpose as MATLAB's fileid.
fid = fopen('GSS1_t001_cx_ImgRall.dat', 'r');
var0a = fread(fid, [1 4], '*uint8');
var0b = fread(fid, [1 4], '*uint8');
var1 = fread(fid, [1 1], '*float');
var2 = fread(fid, [1 2], '*long');
var3 = fread(fid, [1 4], '*double');
fclose(fid);
Note: the above code assumes that the data in the file is the same "endian" as the system running the code. All current releases of MATLAB happen to be on architectures that are "little endian", so the code assumes that as well. If that does not match the file (certainly possible if you got the file somewhere else) then on the fopen() command line, after the 'r', add an addition parameter 'b' (big-endian)
"endian" in files and in memory refers to whether a memory address is to refer to the "Most Significant Byte" or to the "Least Significant Byte" of the data. So if you have two consecutive bytes in memory, such as hex 01 and then hex 02 in that order, and if you are reading 16 bits ("short", int16), is the 01 the most significant byte of the short or is it the least significant byte of the short? To put it a different way, if the file has hex 01 then 02, is the numeric value to be 1 * 256 + 2, or is it to be 1 + 256 * 2 ? Just like writing coefficients of a polynomial, do you write the coefficient of x^0 then the coefficient of x^1 then the coefficient of x^2 and so on, or do you write the coefficient of x^2 followed by the coefficient of x^1 followed by the coefficient of x^0 ? The two orders are not compatible with each other, but are both internally inconsistent. "Little endian" architectures like all of the x86 and x64 architectures, would say that the order in memory should correspond to 1 + 256 * 2 = 513, whereas "Big endian" architectures would say the order in memory should correspond to 1 * 256 + 2 = 258 . The Internet standards say that when data is transmitted between systems where the byte order is not specifically indicated, that "big endian" order should be used, but that is not the same as what Intel processors use. So if you got your file from outside, such as from a research site, it might be in big-endian order.
  9 Comments
Walter Roberson
Walter Roberson on 12 Mar 2016
Those would be uint8 values, with the pattern 0 0 0 0 0 0 248 255 repeated, and that happens to be the pattern for nan in little endian. The simplest explanation is that the data is indeed nan.
OldCar
OldCar on 17 Mar 2016
you are write. Thank you for your help

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!