Matlab and IDL Data Types

조회 수: 14 (최근 30일)
Kash Costello
Kash Costello 2020년 1월 30일
댓글: Steven Lord 2020년 1월 31일
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!

답변 (1개)

Steven Lord
Steven Lord 2020년 1월 30일
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
Kash Costello
Kash Costello 2020년 1월 31일
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 2020년 1월 31일
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.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by