Matlab and IDL Data Types

11 views (last 30 days)
Kash Costello
Kash Costello on 30 Jan 2020
Commented: Steven Lord on 31 Jan 2020
Hi guys! So I'm having some problems with the data types in Matlab and IDL.
So, in IDL, it automatically identifies the data type of numbers. Like for example, the number -32768 is considered long but when you type this in Matlab, and you put the command "class" or "whos", it's considered as double.
Is there a way in Matlab to figure out the data type the way in IDL?
Thanks a lot!

Answers (1)

Steven Lord
Steven Lord on 30 Jan 2020
The default data type in MATLAB is double.
There are some functions and functionality that by default create variables of different types. Notable among them are true and false which create logical arrays, the conversion functions int8, uint16, etc., and the ability to specify hex literals introduced in release R2019b.
x = 0xbeef % x is a uint16 with value 48879
If you want to know the type of a particular variable use class. If you want to know if a variable is a something, you can use isa (which offers options to check if something is stored as a numeric class, a floating-point class, or an integer class.)
  4 Comments
Kash Costello
Kash Costello on 31 Jan 2020
Okay, let me try to do it again.
In IDL:
> a = -35777
> help,a
> A LONG = -35777
It automatically tells me the data type.
===============================================
In Matlab,
a = -35777 will give me double (if I use class or whos), since that's the default of matlab.
Now, in IDL, I didn't do anything else. I just typed the number and it knows that the data type is long. In Matlab, you have to do like uint8(a) to have the format that you want but I don't want that. I want Matlab to tell me that -35777 is long automatically, the way IDL does. Just type the number and boom, it will tell you it's long. That's why I mentioned that I'm willing to write a function with input as the number and output as the data type, but that I have no idea where or how to start.
I hope this is clearer on what I am asking. I'm not sure if what I want is possible to do in Matlab, though.
Thanks!
Steven Lord
Steven Lord on 31 Jan 2020
As of release R2016b, according to this entry in the Release Notes, the header information displayed for many data types includes the name of the class of that array. double and char are listed as not displaying that new header, however.
There's no option that I'm aware of to enable that header for double arrays. While you theoretically could overload how double arrays were displayed I would strongly discourage that as it would affect how every double array was displayed and some functions may be depending on the standard display of double arrays.
The general rule of thumb: when you display the contents of a variable, if you don't see that header and it's a number, it's a double.
If you don't see that header and it's text data, it's a char.
If you see that header, it's whatever the header says it is.
Class authors can control how instances of their classes are displayed (which is why that's a rule of thumb and not a rule.)
If you want a function that accepts the number as input and returns the type of the input, you don't need to write your own. The class function built into MATLAB does exactly that. The whos function may also be of interest to you, if you want to get some information (name, class, size, and attributes) about a bunch of variables at once.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!