Getting the Matlab Plugin Working with TeXmacs

Finally, I got the Matlab plugin running on TeXmacs 1.99.18. If any one is interested in, I’m more than willing to share. I could possibly also write an matlab-install.en.tm or matlab-demo.en.tm for the official documentation, if this were appreciated.

Cheers
Tilda

I am interested :slight_smile:
For the officlai documentation, perhaps @darcy can tell you how to submit a pull request (either on Github or on Savannah).

I believe pull requests for plugins can be sent here:

1 Like

The following has been tested on OSX 10.13.6 and 10.15.7 working with TeXmacs 1.99.18 and MATLAB_R2020a.app. It should work in a similar way on other Unix systems and more recent versions of Matlab. In a nutshell, what I did was the following:

  1. to make sure that the Matlab plug-in finds the startup script contained in MATLAB_R2020a.app, I created a softlink to it by issuing

sudo ln -sf /Applications/MATLAB_R2020a.app/bin/matlab /usr/local/bin/matlab.

  1. now that I have matlab in my PATH, the sessions menu will contain Matlab; clicking it will start a Matlab session, containing however some error messages when trying to plot some functions. The reason being that the original plug-in startup tm_matlab invokes Matlab by the -nojvm option.

  2. In more recent versions of Matlab the -nojvm option starts Matlab without the jvm software which “… restricts functionality. Features that require Java software (such as the desktop tools and graphics) are not supported”. (See: https://de.mathworks.com/help/releases/R2020a/matlab/ref/matlabmacos.html?searchHighlight=-nojvm%20Startup%20Option&s_tid=doc_srchtitle).

  3. So, I modified tm_matlab in {TEXMAX_HOME}/plugins/matlab/bin/ in this way:

  • replacing #!/bin/sh by #!/bin/bash as built-in echo of sh does not support the -ne option.

  • replacing matlab -nojvm -r tmrepl by matlab -nodesktop -nosplash -r tmrepl.

  1. This starts Matlab in TeXmacs and you can do
  • numerical calculations

  • 2d and 3d plotting, without getting error messages like Error using gcf, Error using plot, Error using plot3 anymore.

Executing the plotting session will open a Matlab figure-window and you can save the plots to disk and then insert/link the image (in)to your TeXmacs file.

Closing a Matlab session within TeXmacs shuts down the app by causing an “abnormal termination” due to a “segmentation violation” creating matlab_crash_dump.xxxx-y-files in $HOME. In order to prevent this from happening, it is better to close the Matlab-app on the desktop.

Cheers
Tilda

3 Likes

Thanks for sharing this, @Tilda!

The Octave plugin is more developed than the Matlab plugin. It has inline plotting, for example. I wonder if it would take much effort to make the Octave plugin Matlab-compatible. I don’t have access to Matlab to test, unfortunately.

Thanks a lot for the work. It also motivated me to figure out why I could not use the Matlab plugin and I figured out that there is an additional issue.
If you have a Matlab startup script, and in this startup you change directory, then Matlab won’t find the tmrepl script it needs (which is in $TEXMACS_PATH/plugins/matlab/code).

The ways to get around this that I found, though, are through Matlab and not through TeXmacs (so they cannot be included in the plugin).
Either

  1. Add the $TEXMACS_PATH/plugins/matlab/code to the Matlab path permanently

or

  1. Make it so that startup runs differently if you are invoking Matlab from tm_matlab.

To do this, I helped myself with this Matlab Answer. One has to set an environment variable, and the startup file has to inquire its value.

In doing this, I also learned that in Linux there is a difference between setting an environment variable and exporting it—the latter action means making it available to all process started from the given shell.

P.S. Looking forward to your modifications being included in the TeXmacs code.

One could also write the full path to tmrepl in tm_matlab (so, @Tilda, if you are going to submit a pull request for the Matlab plugin, you could include this change too).

Please create pull requests to

Just set up the repo for you. @Tilda

Thanks for your comments and suggestions @pireddag!

After modifying two lines, as previously mentioned, my plug-in startup tm_matlab looks like this:

  #!/bin/bash
    echo -ne "\002verbatim:"
    cd $TEXMACS_PATH/plugins/matlab/code
    matlab -nodesktop -nosplash -r tmrepl

So, is this what you had in mind when you wrote that

As I don’t use any other special Matlab startup scripts ( startup.m etc.), this works fine with me. Maybe s.th. like the following makes launching the Matlab-plugin a bit more flexible:

#!/bin/bash
echo -ne "\002verbatim:"
MATLAB_PATH=/Applications/MATLAB_R2020a.app/bin/matlab
PLUGIN_CODE=$TEXMACS_PATH/plugins/matlab/code
exec ${MATLAB_PATH} -nodesktop -nosplash -r ${PLUGIN_CODE}/tmrepl

This starts Matlab. However, it doesn’t read-in tmrepl as I get the following error message:

/Applications/TeXmacs.app/Contents/Resources/share/TeXmacs/plugins/matlab/code/tmrepl
|
Error: Invalid use of operator.

Consequently, the Matlab-session also freezes. Maybe you have an idea how to fix this.

Cheers, Tilda

I also tried this one:

#!/bin/bash
 echo -ne "\002verbatim:"
 export PLUGIN_CODE=$TEXMACS_HOME_PATH/plugins/matlab/code/
 export MATLAB_BASH=/Applications/MATLAB_R2020a.app/bin/
 MATLAB_PATH="$MATLAB_BASH:$PLUGIN_CODE"
 exec matlab -nodesktop -nosplash -r tmrepl

This again starts Matlab, but it doesn’t read-in tmrepl as the error message shows:

Unrecognized function or variable 'tmrepl'.

Tilda

Thanks, @Tilda. I believe that MATLAB interprets what follows the -r switch as a command, so it sees the / in the path as a division operator.

It may work with the -sd switch which specifies the startup folder.

Seems like the environment variable should be MATLABPATH, not MATLAB_PATH:
https://uk.mathworks.com/help/matlab/matlab_env/what-is-the-matlab-search-path.html

Thanks a lot @jeroen!

using the -sd switch was the ticket. So the plug-in startup tm_matlab looks like this now:

   #!/bin/bash
     echo -ne "\002verbatim:"
     export PLUGIN_CODE=$TEXMACS_HOME_PATH/plugins/matlab/code/
     export MATLAB_BASH=/Applications/MATLAB_R2020a.app/bin/
     MATLABPATH="$MATLAB_BASH:$PLUGIN_CODE"
     exec matlab -nodesktop -nosplash -sd ${PLUGIN_CODE} -r tmrepl

Definitely not the most elegant code but it works. :slightly_smiling_face: I guess @pireddag should be able to adjust the location of files and directories easily to his Linux needs.

Cheers, Tilda

Hi again @Tilda.
IMHO the plugin code needs to take into account the startup.m. In my “vast” experience (I have talked to only one other person about Matlab startup!) startup.m is used by many people … apart the joke on the vast experience, it is a sensible thing that the plugin code takes startup.m into account.

One of the things that the startup may do is to change the initial Matlab directory, and I do not know whether the -sd switch supersedes that, as I have Matlab R2011b which does not support it so I can’t test it (my guess is that it does not, I assume that startup is run after -sd fixes the directory).

Moreover, I feel that users would like to start from the directory that they defined in the startup.

My proposed tm_matlab is therefore

#!/bin/bash
echo -ne "\002verbatim:"
PLUGIN_CODE_PATH=$TEXMACS_PATH/plugins/matlab/code/
export MATLAB_BASH=/Applications/MATLAB_R2020a.app/bin/
export MATLABPATH="$MATLABPATH:$MATLAB_BASH:$PLUGIN_CODE_PATH"
matlab -nodesktop -nosplash -r tmrepl

where

  1. I have added the PLUGIN_CODE_PATH to the MATLABPATH (recursively, so that if the user already has a MATLABPATH, we add to it)
  2. I have exported the MATLABPATH, so that the matlab process will see it (as far as I understand, you have to export environment variables so that child processes (I hope this is the correct expression) can see them.

Besides this, I did not understand what the MATLAB_BASH variable does, I have therefore left it there.

Finally, for invoking Matlab commands which include slashes, you can include them in quotes. An example is

matlab -nodesktop -nosplash -r "addpath(fullfile(getenv('TEXMACS_PATH'),'plugins/matlab/code')); tmrepl"

which I did not use in the script because I liked the one with MATLABPATH better :slight_smile: .

Pls. let me know if this looks sensible.
Giovanni

1 Like

Thanks for the clarification @pireddag! I agree, using -sd may not result in what a user would expect. Your solution seems a good one.

I don’t think MATLAB_BASH needs to be on the MATLAB search path. It doesn’t contain any .m files, so it doesn’t need to be searched.

Dear @pireddag,

thanks for the significant improvements of the startup script and your concise explanations. The MATLAB_BASH variable is some code junk that’s been left over from some previous attempts, and, as @jeroen has rightly remarked, doesn’t contain anything useful and should no longer be in the script.

So, the cleaned-up version of your tm_matlab is therefore:

echo -ne "\002verbatim:"
PLUGIN_CODE_PATH=$TEXMACS_PATH/plugins/matlab/code/
export MATLABPATH="$MATLABPATH:$PLUGIN_CODE_PATH"
matlab -nodesktop -nosplash -r tmrepl

I’ve tested it; it reads in startup.m, and works very, very nicely!!

Thanks again, @pireddag and @jeroen for your expertise.

Cheers, Tilda

1 Like

Looking forward to your pull request :slight_smile: (I am not part of the developers’ team, @darcy will look after it).

Dear @pireddag,

I’ve finished writing the documentation files, and I’ve also set README.md on GitHub. But now I’m stuck and don’t know how to upload the matlab*.tm files.

Tilda

Hi @Tilda,
I think the way to send the pull request is the following.
First fork the repository to which you would like to send the pull request (in this case https://github.com/texmacs/matlab); then modify your fork; then open a pull request from your fork.
If you are working on your fork, you can upload your files by drag and drop.

Please have a look at the README.md of https://github.com/texmacs/notes (section “To contribute”); the steps are the same as far as I understand—and you do not need to work locally, so you don’t need to clone/add/commit on your computer: as I wrote above, drag and drop works, then you can open the pull request through the web interface as well.

I have just a little experience with version control systems and with git in particular: in part by experience and in part by guessing, working locally is quicker and puts more powerful tools at your disposal, but you have to invest some time at the beginning to learn it.

Said this, I expect that you will be able to fork/modify/send the pull request using only the web interface.

Let me know if this works!
G.

Hi again, @pireddag

this is how far I got:

Is this it?

Cheers, Tilda