setvartype does not work

3 views (last 30 days)
Sebastian Starke
Sebastian Starke on 3 Jul 2019
Hello,
i need to convert a workspace variable or even a struct with several variables from standard double to single. This is needed because i use simulink coder for codegen and want to make these parameter variables globally accessable from the code environment. Release is 2018a.
I found the expression:
opts = setvartype(opts,type)
in the documentation, but it won't work. It seems to be a basic function, nevertheless I get the following error-message:
>> density = setvartype(density,'double')
Undefined function or variable 'setvartype'
Am i doing something wrong?

Accepted Answer

Guillaume
Guillaume on 3 Jul 2019
As it says on the 3rd line of the documentation, opts = setvartype(opts,type) "updates all the variables in the opts object ...". Said opts object being an object returned by detectImportOptions. The whole purpose of detectImportOptions and setvartype is to customise the import of text (or excel) files, nothing else.
The way to convert a variable to double, if it's not already (bearing in mind that's the default), is with:
density = double(density);
Whether or not it will solve your actual problem, I have no idea as I don't use Simulink or coder. If it doesn't you would be better off asking in a new question how to solve your actual problem which is how to make parameters globally accessible whatever that means. You probably need to give more details.
  1 Comment
Sebastian Starke
Sebastian Starke on 3 Jul 2019
Hello, thank you very much for your quick answer!
I was aware that I can convert a single variable with the double();single() etc. expression. However, I searched the web for an expression for all variables in a structure and probably didn't read the documentation carefully enough. What was described there as an "object" I simply interpreted as "any workspace variable". Therefore I made a mistake.
The problem regarding the coder thing is just a typecast issue, which is very clear to me. For that i wanted to change the datatype to get rid of that typecast ;)
But, thank you anyway. In the meantime I wrote a short script witch the double() expression which just went through the struct and converted each parameter. Now it went well and the variables are globally accessible.

Sign in to comment.

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!