I have been posting blogs about MATLAB with code examples for many years. Steve Eddins, my fellow blogger of Steve on Image Processing fame, developed and maintains an internal tool that automates a lot of tasks and I rely on it to publish my blog posts.
One of my guest bloggers, Toshi Takeuchi, showed me a new tool he found, and he would like to do a quick introduction.
Hi, it's Toshi again. If you use MATLAB for fun like me, you would probably like to publish your work to share with other people. I do web analysis in marketing and I can tell you there are many people like you out there, based on the increasing search volume for personal use of MATLAB.
I have been writing to our internal WordPress blog service, and most of my posts involve MATLAB code examples. I would first do my analysis in MATLAB, then add commentary to explain what I did, and then publish it to HTML. After that, I had to manually copy and paste the published text and code to WordPress – and the process is tolerable but isn’t particularly easy.
I recently started contributing, as a guest blogger, some of my personal posts for Loren who maintains her public-facing blog. The other day I had a chance to witness how she posts her blogs. While her process was much more efficient than mine, I started wondering if we could do even better.
I suspect that this may be a widely shared pain. So I dug around a bit, and I found this wonderful MATLAB module called matlab-wordpress by John Kitchin on GitHub.
Installation was very straight forward. I just downloaded the zip file, added the extracted folder to the MATLAB search path, and jar files to javaclasspath, as described in README.md.
Next, enable XML-RPC (Enable the WordPress, Movable Type, MetaWeblog and Blogger XML-RPC publishing protocols) in your admin console. This is under:
Settings -> Writing -> Remote Publishing
You can avoid entering your username and password for your inside WP account if you set up blogCredentials.m as described in README.md.
user = 'your blog username'; password = 'your blog password'; server = 'http://your-blog/xmlrpc.php';
If you include an image or generate a plot in your MATLAB code, then the image will be automatically uploaded to the Media Library automatically. Sweet!
figure hist(randn(1000,1),20) title('Normal Distribution with \mu=0, \sigma=1')
Let's say you want to link to a .mat file used in your post, and the file path should be defined relative to the location of the MATLAB file you are publishing. If it is in the same location as your MATLAB file, you can link to it as follows:
"You will need to :download:`sample.mat` for this example."
This will then translates to:
"You will need to download sample.mat for this example."
The file will be automatically uploaded to the same directory that holds all your images and the URL will be updated to reflect the new file location. This alone will save tons of hassle. Unfortunately this may not work if your WordPress server is not configured to accept nonmedia files like .mat files.
You can add a link using MATLAB Markup syntax, but matlab-wordpress also gives you an ability to link to another post on your blog. Since Loren doesn't have this MATLAB module, you won't see the effects of these next sections in this post.
"In :postid:`920` we discussed how to analyze Twitter with MATLAB..."
You can add a tooltip like this.
:tooltip:`<this is the tooltip.> Hover on this`
You can also add color to text to draw attention to a note or warning.
:note:`This is a note in light blue.` :warning:`This is a warning in pink.`
You can add categories and tags to your post if you include the following lines in your code. Currently it appears you can only add one of each, for some reason.
% categories: Blog % tags: MATLAB
Now it's time to publish your MATLAB file to WordPress. If your MATLAB file is myFile.m, then you enter the following command in the command window.
Perhaps you want to publish locally first as a dryrun. If so, instead do:
blogpost myFile.m true % set |dryrun| parameter to |true|
When you publish your post, your post ID and permalink is automatically added to the MATLAB file. If you update your file, you can update your blog post by simply typing blogpost myFile.m again. If you remove your post ID from your MATLAB file, that will result in a new post.
Because I happen to use WP-Syntax plugin on my blog for syntax highlighting, I have to remove the CSS tags generated by publish from MATLAB and make other adjustments related to that. If you can use the MATLAB generated CSS as is, then my guess is that you don't have to do any additional tweaks.
Even though I still had to do some cleaning, the overall process took a lot less time than the manual process I use. Mileage may vary depending on your settings, but I think this is going to be useful for many people. Let us know how it works for you here.