Tuesday, November 30, 2021

Generate C/C++ Code From Matlab Function

Use MATLAB Coder to generate C/C++ code from an m file (must be a function). At the end of the process, click on "View Report" and on the Report Viewer window, click on Package Code to package all the dependencies (e.g. tmwtypes.h) into a single zip file.

Friday, November 26, 2021

Check if the MATLAB instance you created has exited

When you run MATLAB from a Windows bat file and want to know if it has finished its job and exited (without considering other already open MATLAB instance), you can use the following:

start matlab.exe -nosplash -nodesktop -noFigureWindows
set pid=not found
for /f "tokens=2" %%a in ('tasklist^|find /i "matlab") do (set pid=%%a)
echo matlab pid = %pid%

:waitForMatlabToExit
timeout /t /5 /nobreak
tasklist /fi "pid eq %pid%" | find ":">nul
if errorlevel 1 goto waitForMatlabToExit
echo Matlab exited
exit /b

Thursday, November 25, 2021

Windows batch file: goto vs call

In a Windows batch file, goto label2 command goes to the label2 and continues from the end of label2. call :label2 goes to label2 and after seeing a exit /b (or end of script, eof), returns back right after the call command. In other words, call behaves like a function call in Java, C++ etc. but you have to use exit /b instead of return.

Calling a Matlab function from Windows command line (cmd)

Calling a Matlab function from a Windows cmd batch file is easy if the function has no string arguments. However if the function takes a string argument, you have to use two single quotes around the argument. Example:

start matlab -nosplash -nodesktop -noFigureWindows -r eval("'myFunction(''fileName.txt'')'")

Wednesday, November 10, 2021

Remote connection and file transfer

The easiest way to connect from Windows to a remote Linux computer is to fire up cmd.exe and type ssh user@ip where user is the Linux user name and ip is the IP address of the Linux computer. 

For file transfer, you can use sftp user@ip or scp fileName user@remoteIP/remoteFileName.

ssh, sftp and scp come with Windows 10, you don't have to install anything. Just make sure that SSH is enabled on Linux.

Tuesday, November 9, 2021

Simple way/pattern to separate GUI from model

Prepare your model as a separate executable that reads input from file and writes output to a file. Then you can code your GUI in whatever language you like (e.g. Excel macros) that takes input from user, writes them to file, runs the model executable and reads outputs from file when executable finishes.

This completely decouples GUI from implementation. Compared to a DLL, it has a simpler interface, it will not crash your JVM if there is an error in model, and let's you run models in parallel much easier and also facilitates easy batch run of model executable via scripts.

Wednesday, November 3, 2021

Debugging Effort

The hardest part of debugging is finding the root cause of the problem, therefore it is wise to optimize code for debugging, see clean code:


Tuesday, November 2, 2021

memcpy() vs memmove()

When you need to implement removal of an element from an arraylist, don't use memcpy, use memmove. Your program might run fine on Windows but cause trouble in Linux, whose root cause would be hard to find.


memcpy() vs memmove(): in case of memcpy(), there is no extra buffer taken for source memory. The copying is done directly on the memory so that when there is memory overlap, we get unexpected results

Monday, November 1, 2021

Simulink versions

Matlab release - Simulink version (helpful when you know the Simulink version and wonder which Matlab release it corresponds to):