Change data type in matlab

74 views (last 30 days)
Diana Acreala
Diana Acreala on 3 Apr 2012
I'll present my question with an example.
I create parameter: x=1; now we have the possibility to see in workspace, among many others, the class of this parameter which automatically will be 'double'.
I have this parameter in a Simulink model, where I use it as an int8. Next I want to determine the byte size for this parameter x.
But the problem is that in Matlab workspace the data type of parameter x is double. I need to change the class from double to int8 (adapt it as in the Simulink model).
How can I do this considering that x=1 is in workspace as a parameter and I have another 2 variables: var_param_name=x (type char) and var_data_type=int8 (type char). I managed to obtain these 2 variables from workspace with find_system command.
Thanks! Diana

Accepted Answer

Acreala Anca
Acreala Anca on 3 Apr 2012
So you have a = 1 in you workspace which is a double. You also have b = 'int8' which is a char.
I think you can use the eval command which will execute a string command. So this means that you have to create your command as a string.
c = strcat(b,'(',int2str(a),')')
This will actually store into your workspace a 'c' string with the value:
c = 'int8(1)'
Now you can simply pass this string to the eval command.
So a = eval (c), and there it is:
a = 1, as an int8. :D
Good luck!
Anca Acreala
  1 Comment
Diana Acreala
Diana Acreala on 3 Apr 2012
Thank you very much for your help!! :D

Sign in to comment.

More Answers (2)

Kaustubha Govind
Kaustubha Govind on 3 Apr 2012
Not sure if I understand your question correctly, but to create an int8 value in MATLAB, you simply need to use:
>> x = int8(1);

Diana Acreala
Diana Acreala on 3 Apr 2012
I need to use the two variables: var_param_name=x (type char) and var_data_type=int8 (type char); Can't do x = int8(x); directly...


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

Community Treasure Hunt

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

Start Hunting!