MATLAB.executable + UE4 C++ ?

Hello mates.

I have a working MATLAB code base that generates sounds ( . wav format) depending on parameters entered. Instead of using it separately and loading .wav files in the editor by hand I wanted to make it work in UE4 on C++ side. MATLAB can give me an executable and a dll but am not exactly sure how to utilise them.

A friend with some experience suggested using something called “Shared Libraries”. I am checking that out now. Here it is.

So if you could direct me to right resources for this or simply share your similar experiences I would be really grateful.

Thanks a lot mates !

I can’t comment on matlab, but loading DLLs is quite easy once you know how. There’s no super great examples though, so here’s what I did:

  1. Put the matlab .dlls and .dat files into your project’s Binaries folder, next to the .dll or .exe that it creates for your project. If you’re making a plugin, put it in your plugin’s binaries folder instead.
  2. Add your header files (matlab’s *.h files) into <your project>/ThirdParty/MatLab/Includes/ (The rule here for this one and the next step is the ThirdParty folder lives at the same level as your Source folder. If you’re writing a plugin, put it next to the plugin’s source folder.)
  3. If there’s a Lib file (there should be) put it into <your project>/ThirdParty/MatLab/Lib/Windows/Win64/ (or whatever your platform is.)
  4. This goes into your <myproject>.Build.cs file:

 private string ModulePath        {
            get { return Path.GetDirectoryName(RulesCompiler.GetModuleFilename(this.GetType().Name)); }
        private string ThirdPartyPath
            get { return Path.GetFullPath(Path.Combine(ModulePath, "../../ThirdParty/")); }
        private string BinariesPath
            get { return Path.GetFullPath(Path.Combine(ModulePath, "../../Binaries/")); }

        private string LibraryPath
            get { return Path.GetFullPath(Path.Combine(ThirdPartyPath, "MatLab", "Lib")); }

        public bool LoadExternalLib(TargetInfo Target)
            PublicAdditionalLibraries.Add(Path.Combine(LibraryPath, "Windows", "Win64", "octane.lib"));
            return true;

And then from your main function in that file, call:


Make sure to replace the paths and the word “MatLab” with whatevcer they should actually be. What happens is the compiler loads the lib, which finds the dll in your binaries folder and then uses the header files to validate your code so that it will build.

You use the header files supplied like documentation. Check out the namespace the header uses and make calls that way. Best to find some matlab specific examples if you can. It’ll end up looking bit like this example I just made up:

MatLab::Waveform = new MatLab::Waveform(Inputargument1, InputargumentB);

Pick a real easy command from the header files to test so that you can verify it’s all linking up.

One thing to remember is that calls to DLLs are blocking. Your program will have to wait for a response before it can continue, so it’s a good idea to thread them somehow but I haven’t done this myself yet.

You’ll also need to rejigger this with compiler directives to handle the “Windows” part of the path and turn it into “Mac”. There are other examples around but check out fbxsdk.h in the engine code.

WOW. Thanks mate. You are a life saver ! :smiley:

No worries. The only main problem I’ve had with DLLs is everything in them uses data types and standard library functions that UE doesn’t normally use, so getting data between them (particularly string data) was kind of tough and confusing at first. This is the most useful page for converting strings:,_FString_to_Int32,_Float_to_FString

The other super common one is to get a char array out of an FString do:

FString myString = FString(“Stuff”);
(const char *)*myString;

hey there is there an easy way for blueprint coders like this one. i think this is for C++ users. thanks!