pass a vector from matlab to a c++ program
이전 댓글 표시
function res = fun(p,y)
s = call_a_cpp_program_which_operates_on_p
res = s - y;
end
In the above function, 'p' is a vector and 's' as well. I have to pass the vector 'p' to a c++ program to compute 's'. The above function is called several times.
My idea is to first write the vector 'p' in a file, which the c++ program reads in. The c++ program writes its output 's' also in a file, which MATLAB can read in.
Probably this would work, but is there a better approach to transfer the vector 'p' from MATLAB to my c++ program?
댓글 수: 9
Torsten
2022년 8월 14일
Just out of interest: Which PDE solver do you use as the c++ program in the background ? Some commercial tool like ANSYS ?
SA-W
2022년 8월 14일
I use an open source library called dealii. Basically, my program is a class. Creating an instance of that class in the main function will call several member functions which eventually compute 's'. I want to store 'p' as a member variable of that class.
Torsten
2022년 8월 14일
Sounds too complicated for a coupling by direct data transfer. I think it will be a challenge to coordinate data transfer even over written files. One solver makes calculations - the other has to check when the input data file changes ...
But maybe James Tursa will place a comment.
SA-W
2022년 8월 14일
My idea was to work with simply two files. Whenever the function 'res' is called, the file storing 'p' is overwritten with the 'p' being passed to 'res' and the file storing 's' as well.
Or is it necessary for lsqnonlin to store the 'p's and 's's associated with previous iterations?
Torsten
2022년 8월 14일
Or is it necessary for lsqnonlin to store the 'p's and 's's associated with previous iterations?
No, but you must keep both solvers in a "wait" state until they get new data from the other solver. Do you already have an idea how to handle this ?
SA-W
2022년 8월 14일
I do not have deep knowledge about communication between processes.
But is it not enough to keep only MATLAB in a waiting state while the c++ program is running? Because the c++ program is started by MATLAB (probably using the system command).
Torsten
2022년 8월 14일
Sorry, but I don't know.
SA-W
2022년 8월 14일
I guess the system command is exactly what I need as for the communication between the processes. In the documentation, it says that MATLAB waits for the executed command to be finished, i.e., until my pde solver exits.
But maybe James wants to add another comment below.
Torsten
2022년 8월 14일
In such cases I always say:
The proof of the pudding is in the eating (Probieren geht über Studieren).
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 C Shared Library Integration에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!