Call a C function from Matlab

Hello,
I am trying to call a C function form Matlab. The files are in different folders. How can I achieve that? I am using Matlab R2018b.
The C file is called main.c and its funtion to be called is int main(void). I tried something like this:
% callmain.m
function y = callmain() %#codegen
y = 0;
coder.cinclude('main.h');
y = coder.ceval('main');
end
With the following code I tried to specifiy the location of the files:
cfg = coder.config('lib');
cfg.CustomInclude = [fullfile(pwd,'Inc') ' ' fullfile(pwd,'Src')];
cfg.CustomSource = fullfile(pwd,'Src','main.c');
This line of code is supposed to generate the code:
codegen -config cfg callmain.m
The complete C code can be obtained from the following link:
I would appreciate your help!
Thanks

17 件のコメント

Darshan Ramakant Bhat
Darshan Ramakant Bhat 2020 年 3 月 3 日
Make sure that you have "main.h" and "main.c" files present in the "CustomInclude" directory.
In your codebase I dont see those files inside pwd/Src or pwd/Inc folders.
Sebastian Linn
Sebastian Linn 2020 年 3 月 3 日
編集済み: Sebastian Linn 2020 年 3 月 3 日
Sorry while testing I changed the names of the files and forgot to change them back before uploading.
But unfortunetely that was not the problem, so the problem still exists.
Here are the new files:
Please help!
Thank you very much!
Darshan Ramakant Bhat
Darshan Ramakant Bhat 2020 年 3 月 4 日
I think the problem is there in the way you are including the multiple paths in "cfg.CustomInclude". Please refer to the correct syntax from the below documentation :
Specifically you have to use "pathsep" command to seperate different paths. Make sure that you have included all the relevent directories.
Hope this will be helpful for you.
Darshan Ramakant Bhat
Darshan Ramakant Bhat 2020 年 3 月 6 日
Are you able to resolve the issue ?
Sebastian Linn
Sebastian Linn 2020 年 3 月 6 日
Unfortunetely not!
I have tried the following (also with the line commented out):
cfg = coder.config('lib');
% cfg = coder.config('lib','ecoder',false);
cfg.CustomInclude = [fullfile(pwd,'Inc') pathsep fullfile(pwd,'Src')];
cfg.CustomSource = fullfile(pwd,'Src','main.c');
codegen -config cfg callmain.m
But when I run this script I still get an error message.
Please help!
Darshan Ramakant Bhat
Darshan Ramakant Bhat 2020 年 3 月 6 日
Can you please state the specific error that you are getting ?
Sebastian Linn
Sebastian Linn 2020 年 3 月 6 日
Darshan Ramakant Bhat
Darshan Ramakant Bhat 2020 年 3 月 6 日
From the error it is clear that it is not able to find the "stm32f4xx_hal.h" header file. You need to include all the directories of the required headerfiles properly. Something like below :
cfg.CustomInclude = [fullfile(pwd,'Inc') pathsep fullfile(pwd,'Src') pathsep fullfile(pwd,'Drivers','STM32F4xx_HAL_Driver','Inc') pathsep fullfile(pwd,'Drivers','CMSIS','Device','ST','STM32F4xx','Include') pathsep fullfile(pwd,'Drivers','CMSIS','Include')];
Sebastian Linn
Sebastian Linn 2020 年 3 月 7 日
Unfortunetely this also results in an error.
Here's the message:
Help would be greatly appreciated!
Prasanna Gude
Prasanna Gude 2020 年 3 月 9 日
As I don't have access to the '\LED\codegen\lib\callmain\examples', I am not able to resolve your issue.
Sebastian Linn
Sebastian Linn 2020 年 3 月 9 日
Now try again. It should work now.
Prasanna Gude
Prasanna Gude 2020 年 3 月 9 日
Try building 'mex' function, instead of calling c function from matlab.
Sebastian Linn
Sebastian Linn 2020 年 3 月 9 日
I have tried your suggestion but I get the same error message.
Prasanna Gude
Prasanna Gude 2020 年 3 月 11 日
When I am trying to build mex, I am getting an error like this: error: unknown type name 'TIM_HandleTypeDef'
Did you defined 'TIM_HandleTypeDef' anywhere?
Sebastian Linn
Sebastian Linn 2020 年 3 月 11 日
No, I have not defined it anywhere actively. I used CubeIDE to generate the code for the target device (microcontroller) with the necessary drivers installed. If I nedd to define it somewhere where do I have to define it?
Thank you!
Prasanna Gude
Prasanna Gude 2020 年 3 月 11 日
'TIM_HandleTypeDef' is there in the third party library "stm32f4xx_hal.h". Did you build that library? If not build that library and after that try building 'mex'.
Sebastian Linn
Sebastian Linn 2020 年 3 月 12 日
No, I did not. How do I build that library? Do you mean creating code by specifying the output to lib?

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

回答 (1 件)

Pankhuri Kasliwal
Pankhuri Kasliwal 2020 年 8 月 20 日

0 投票

Yoou can see the following documentation Link. You need to follow the steps over there to call external C function into MATLAB.
  • If you are calling a C Library function then you need to additionally generate the MEX function. You can find a detailed documentation over here.

カテゴリ

ヘルプ センター および File ExchangeIntroduction to Installation and Licensing についてさらに検索

質問済み:

2020 年 3 月 3 日

回答済み:

2020 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by