fscanf/Pointing to a file in MATLAB Coder?

5 ビュー (過去 30 日間)
Ryan Kalaigian
Ryan Kalaigian 2018 年 8 月 17 日
編集済み: Ryan Livingston 2018 年 8 月 17 日
Hello, I am new to MATLAB coder and I am having some trouble translating the way I translate fscanf using coder.ceval. I know codegen supports fopen but not fscanf. Here's what I have:
coder.cinclude("<stdio.h>");
f = fopen(z,'r');
f = coder.opaque('FILE*','NULL');
coder.ceval('fscanf',f,formatSpec, A); %should read in values into 2-by-x double array
fclose(f);
z is a string and the name of the file, formatSpec is '%d %d' and A is the appropriate size. How do I get the pointer to the file to be z? Any suggestions would be appreciated.

回答 (1 件)

Ryan Livingston
Ryan Livingston 2018 年 8 月 17 日
編集済み: Ryan Livingston 2018 年 8 月 17 日
Check out the example:
which shows using fread via coder.ceval. You should be able to switch:
f = coder.opaque('FILE*','NULL');
f = fopen(z,'r');
Also don't forget to NULL-terminate your strings before passing to C. That example uses a helper function:
% Create a NUL terminated C string given a MATLAB string
function y = c_string(s)
y = [s 0];
like so:
f = fopen(c_string(z),c_string('r'));
You'll need to preallocate A before reading into it and call coder.ceval('fclose',f) like the documentation example shows.

カテゴリ

Help Center および File ExchangeMATLAB Coder についてさらに検索

タグ

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by