user2212608
user2212608

Reputation:

automatically assign class type in matlab

I'd like to obtain a class type of a variable and use it as a function in Matlab.

For example, say x is of class uint8. I can obtain this info by classtype=class(x).

What I'd like is to use it on a different variable automatically, such as:

y=classstype(y)

where y is of type logical for example.

How can I accomplish that?

Upvotes: 1

Views: 68

Answers (2)

freude
freude

Reputation: 3832

The function class() returns the string with a class name. You can use it further using the function eval() which deals with strings as an input.

Upvotes: 0

Ryan J. Smith
Ryan J. Smith

Reputation: 1140

It sounds like you're trying to cast the value of y to a different class. To this end, you could try using Matlab's cast() function.

In your specific instance, you could try:

y = cast(y, class(x))

This should get the class of variable x and cast variable y to that class.

Upvotes: 4

Related Questions