How to convert to C a function with variable fields in the input?

1 回表示 (過去 30 日間)
PatrizioGraziosi
PatrizioGraziosi 2020 年 9 月 28 日
コメント済み: Walter Roberson 2020 年 9 月 29 日
Hello everybody,
I wish to convert to C a function that have two inputs, a scalar and a file or a struct
[outputs] = myfunction(id, 'myfile.mat') ;
or
[outputs] = myfunction(id, mystruct) ;
myfile.mat or mystruct contain fields which can have different names according to the user's inputs, which are checked by the the function according to the isfield command.
However it looks I cannot define an input type 'file' in the Matlab coder... nor I can leave a struct input without defining all the field names...
Any suggestions, please?
Thanks
Patrizio
Edit: original question modified to make it more focused

採用された回答

Mario Malic
Mario Malic 2020 年 9 月 28 日
編集済み: Mario Malic 2020 年 9 月 28 日
You can send a variable that contains full path to the file as an input argument and within the function use load command.
Edit: though I haven't used MATLAB Coder before, so I am not sure whether this will work.
  2 件のコメント
PatrizioGraziosi
PatrizioGraziosi 2020 年 9 月 29 日
Hi Mario,
thanks for your time! The function should run into a parfor loop so I set the file as shared in all the workers before the parfor to avoid the file to be transferred continuously from master to workers...
However, I have found that I can input one struct data instead of the file, but the problem is still there as the structure fields cannot be all defined in advance, before the user eneters the inputs...
Can someone suggest a strategy?
Walter Roberson
Walter Roberson 2020 年 9 月 29 日
C and C++ do not have dynamic field names. You cannot create a struct or a union with dynamic field names in C -- you could only create a datatype in which there was a list of field names and a list of corresponding pointers to data.
I do not have experience with generated MATLAB code so I do not know if MATLAB Coder provides the interface to the structure maintenance API that you would call from mex routines.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by