How can I use a coder.opaque value in a Matlab expression with codegen?

6 ビュー (過去 30 日間)
Sean
Sean 2012 年 10 月 31 日
コメント済み: Sean 2016 年 9 月 21 日
Hi
I need to read in some parameter files for my system. I am using the example in the help files for coder.opaque to declare the file ID value. I would like to check the ID to see if the fopen call completed successfully, but I keep getting an error saying it will not accept a coder.opaque data type for the comparison:
% Declare 'fid' as an opaque type 'FILE *'
fid = coder.opaque('FILE *', 'NULL');
fid = coder.ceval('fopen', cmName, ['rb' 0]); % 0 term strings for c
if 0 == fid
coder.ceval('exit', '-1');
else
:
:
The error occurs at the line where I compare the fid to 0. I have tried several alternatives (assigning fid to another var, casting fid to a double, making a function to do the test) but they all fail with a variation of not allowing a comparison with a coder.opaque type.
I imagine that others have run into this problem, but I did not see any entries for it in the database.
Any help would be appreciated.
Thanks Sean

採用された回答

Ryan Livingston
Ryan Livingston 2016 年 9 月 21 日
Note that as of MATLAB R2013a fopen, fclose, and fprintf are supported for codegen. Other file I/O functions have been added more recently so you may just be able to use those directly in MATLAB without using coder.ceval at all.
If you decide to use coder.ceval, you can do this:
fp = coder.opaque('FILE *', 'NULL');
fp = coder.ceval('fopen',...);
NULL = coder.opaque('FILE *', 'NULL');
if fp == NULL
% Something went wrong
else
% All good
end
  1 件のコメント
Sean
Sean 2016 年 9 月 21 日
Thank you - this answer is exactly what I needed.

サインインしてコメントする。

その他の回答 (2 件)

Nils Lande
Nils Lande 2016 年 9 月 21 日
編集済み: Nils Lande 2016 年 9 月 21 日
coder.eval('(int)', fid) can be used to return the value as integer.
Example:
fp_int = int32(0);
fp = coder.opaque('FILE *', 'NULL'); % Declare the FILE pointer
fp = coder.ceval('fopen', szFileName, szMode);
fp_int = coder.ceval('(int)', fp);
if(fp_int == 0)
coder.ceval('printf', ['fopen returned NULL' char(10) 0]);
return;
else
coder.ceval('printf', ['File opened' char(10) 0]);
...
  1 件のコメント
Sean
Sean 2016 年 9 月 21 日
Thank you for your help. This is very similar to the following solution.

サインインしてコメントする。


Kaustubha Govind
Kaustubha Govind 2012 年 11 月 5 日
I don't think coder.opaque type variables can participate in MATLAB expressions. You might need to convert that expression to another coder.ceval call.

製品

Community Treasure Hunt

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

Start Hunting!

Translated by