setvartype does not work

조회 수: 5 (최근 30일)
Sebastian Starke
Sebastian Starke 2019년 7월 3일
댓글: Sebastian Starke 2019년 7월 3일
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?

채택된 답변

Guillaume
Guillaume 2019년 7월 3일
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
Sebastian Starke
Sebastian Starke 2019년 7월 3일
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.

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

추가 답변 (0개)

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by