edovanveen / matlabneuroninterface

Interface for connecting NEURON and MATLAB
BSD 3-Clause "New" or "Revised" License
1 stars 0 forks source link

Redirect stdout en stderr to matlab command window #4

Open AljenU opened 2 years ago

AljenU commented 2 years ago

Instead of redirecting to file, it is probably possible to redirect to the matlab command window.

We would need to connect to matlab from c(++) code: https://nl.mathworks.com/help/matlab/apiref/matlab.engine.connectmatlab.html Redirect the output streams in the c code (many stackoverflow questions) And make it so the redirected stream is shown in the command window: https://nl.mathworks.com/help/matlab/matlab_external/displaying-output-in-matlab-command-window.html Where instead of explicit call to displayOnMATLAB, that should be wrapped in the streambuf sync.

AljenU commented 2 years ago

Part of this workflow can maybe be replaced with a piggyback on an existing neuron stdout redirection for python. That shold be looked into first.

edovanveen commented 2 years ago

I have tried to use mex functionality by giving generateLibraryDefinition the argument IncludePath=fullfile(matlabroot, "extern", "include").

If I then try to import "mex.h" (for using mexPrintf), I get the error: error: Using MATLAB Data API with C Matrix API is not supported.

Importing "mex.hpp" and "mexAdapter.hpp" gives:

\extern\include\cppmex\detail\mexFunctionAdapterImpl.hpp(38): error: incomplete type is not allowed
AljenU commented 2 years ago

Ah, for the c++ version, we should indeed do the c++ version, which is calling fprintf in the matlab session, according to https://nl.mathworks.com/help/matlab/matlab_external/displaying-output-in-matlab-command-window.html

So, we are actually doing something like https://nl.mathworks.com/help/matlab/getting-started-with-matlab-engine-api-for-c.html, which means we have different headers that should be included, #include "MatlabEngine.hpp" and #include "MatlabDataArray.hpp". Thus also different include and library paths are involved, as per https://nl.mathworks.com/help/matlab/matlab_external/build-c-engine-programs.html

AljenU commented 2 years ago

Thus, we would have a 'global' variable for the pointer to the matlab engine, need to find the engine once, then in the custom stream print function do the same as in the displayOnMATLAB function in the beforementioned displaying-output example.

AljenU commented 2 years ago

For now, would keep the input only as string. Later, might want to differentiate between stdout and stderr. Will have to do something slightly different for the std::vector creation then.

edovanveen commented 2 years ago

First (unsuccessful) try on a new branch exploration_connect_MATLAB_session: https://github.com/edovanveen/matlabneuroninterface/tree/exploration_connect_MATLAB_session