Setup Sublime Text or alternative editor for Matlab? - matlab

I did not find any suitable answer or hint, so I am asking this here now: Is there any alternative editor for Matlab with a dark theme (easier to the eyes), that supports integration in Matlab for proper debugging, autocompletition etc.? And if so, how do I setup those editors on windows?
I am really annoyied of the default matlab text editor. The color theme can not be applied to the whole IDE and the syntax highlightning is really bad, because it can not distinguish between numbers, brackets, operation-symbols (=,+,- etc.) and more like that. It is sometimes really hard to read a matlab file in the default editor, because the lack of these basic features.
Thanks!

Looking back at my own question, I will try to give an answer. There is no way to define individual colors for matlab-code in matlab itself. Therefore I stick with Notepad or Atom, which have far superior color syntax even for matlab code. For running the code, you have to switch to matlab of course. This for sure no optimal solution, but the best to get better readability for matlab code.

Related

coloring functions in Matlab script: native vs. custom functions?

Is it possible to show functions in a Matlab script, such that 'built-in' functions appear in one color, and custom functions (home-grown) appear in another color?
histc % appears in 'green'
myOwnFxn % appears in 'purple'
As far as I know, it's not possible to apply a specific color to function names. If you head to Preferences -> Colors -> Programming Tools and to Preferences -> Editor/Debugger you can pretty much discover by yourself what can be customized and what cannot be customized.
On the top of that, there is the problem of distinguishing between build-in functions and a used-defined functions, which is very hard to solve programmatically and nobody knows if the syntax highlighting framework of Matlab can handle such distinction.
Of course, language syntaxes must be defined somewhere inside the Matlab package. Finding them, learning how to manipulate them (if possible) and forcing Matlab to accept a new one can be very time consuming. If you plan to break the rules and do some reverse engineering to analyze the feasibility of your task, I wish you all the best.

Alternatives to extrinsic functions such as imread and other functions during code generation in MATLAB

As you may know, extrinsic functions are not outputted during the code generation process. Are there alternatives to these functions and/or solutions to this problem? My code generation error report is shown below :
Code Generation Error Report
I am surprised that I can't output size and rgb2gray either. Since these are essential to my program, I cannot avoid them.
Help will be much appreciated!
This is a good question, and I see similar questions fairly frequently. As I started using MATLAB Coder, one of the biggest pitfalls was the constant search for supported functions. I sympathize with your frustration, and I have a few tips, having been through this.
First, to your direct question, while imread isn't supported by Coder, size and rgb2gray are. Probably Coder is complaining about these because they have been passed mxArrays from the call to imread, which is fine when it is extrinsic, but not ok for separate generation. That's just a guess. A very useful tool in writing code is the list of Coder supported functions: List of Functions supported in MATLAB Coder
But even with those two, to replace imread is not a tiny task. You'll have to find another library that supports the particular file you're working with, and then stitch that in using coder.ceval. Alternatively, if you can find a pure MATLAB implementation of it, that might help.
Are you targeting a pure C library or a MEX file? If you intend to use this code within the MATLAB environment, you can always use imread separately and then pass the data.
And now to some more general observations: MATLAB Coder isn't a perfect MATLAB to C translation system. It's extremely powerful, and I've been able to write some very large projects with it, but if what you want is the ability to run any MATLAB code without MATLAB around, you should look at MATLAB Compiler, a different add-on. There's a very good Q and A about this here: MATLAB Compiler vs MATLAB Coder
When writing projects in MATLAB Coder, it's really best to start from scratch, knowing you're targeting C code ultimately. There are so many gotchas in the conversion from MATLAB to C that you have to be always vigilant while writing the MATLAB code.
One tool that helps is to right-click on a file in the "Current Folder" list that usually resides on the left-side of the main window, and select "Check Code Generation Readiness." You'll get a great report of potential problems in the file. I recommend using this often.
Another useful tool is to always put the %#codegen tag into your code. This alerts the MATLAB editor that the .m file is intended for code generation, so it provides extra context-sensitive information while you're writing the file. This helps enormously.
The most commonly missing functions for code generation are file IO functions. There are some good reasons for this, but it's frustrating nonetheless.
When you stitch in external C code, you use the coder.ceval function, which can provide excellent access to external libraries. Using this well is a whole other topic, outside the scope of this question.
If you can indicate exactly what kind of files you're interested in reading (PNG, BMP, TIFF, etc.) perhaps someone may be able to identify a good external library for you to use.

Separate Matlab Editor Module?

Is it possible to have Matlab editor, without having the whole Matlab?
The warnings that Matlab editor provides are useful for me in coding, but I can not afford buying Matlab. How can I only use the editor? Does it still give warnings?
You could use GNU Emacs. Configure Emacs Octave Support. Then M files, within Emacs, will have syntax highlighting and indentation. You will not have in-editor warnings as in the MATLAB editor.
You could use Octave to run and test your M files. For simple scripts and functions, little to no modifications will be required to go from MATLAB to Octave, or vice versa. The same is not true for MATLAB code that implements GUIs or some of the more specialized toolbox.
You could certainly use a text editor to edit matlab files, as long as you save them with the .m extension. Unfortunately, as Matlab is the only program that can interpret the file, you won't get warnings as you work.
As an alternative, you could get FreeMat (http://freemat.sourceforge.net/) and work with .m files with proper syntax highlighting. However, FreeMat does not have the vast library of functions that Matlab has, and still won't provide you with warnings as you code.
Maybe you could work with Matlab in your institution's library (if you're a student), or get your business to pay for it (if you're a professional)?

Matlab: Write text to PDF

I'd like to create a PDF out of my matlab m-function. The PDF should contain some text information which I want to style a bit and one image (which is previously generated as figure). Is there any way? The only thing I found is publish to publish source-code. The only alternative I could think off was to programm the texts into the figure window and than export the whole figure to pdf. Perhaps there's a better way to do it?
Thanks!
I would recommend generating your figure with Matlab, outputting it to eps, then converting the figure to pdf using epstopdf. Then embed your figure in a latex document and generate a pdf with pdflatex.
I know this sounds like an incredibly roundabout way of doing it, but
This is the way I've come to do it after years of experience and it always gives me the best results
Every one of my colleagues does it this way for the same reason
The results will be completely reproducible
You're using Matlab to do what it's good at (making scientific figures) and latex to do what it's good at (formatting documents)
The Matlab code to make the eps of the figure would be like this (supposing your figure is figure 1):
print -depsc2 -f1 -loose my_fig.eps
You could pretty easily write a latex template that uses my_fig.pdf and then run everything from your Matlab using bangouts:
!epstopdf my_fig
!pdflatex mydoc.tex
If you're on Windows, you can connect to Word using 'actxserver', insert and style any text you like into a blank document, copy and paste MATLAB figures into the document, and then save it to PDF. You can do all of that from within MATLAB. The first time you do this it's a pain, as you need to learn quite a bit about the Word Object Model; but once you've done it a couple of times it's very simple and quick, and you can achieve very professional results. You can combine this with using the 'export_fig' that others have mentioned.

Using MATLAB's plotting features as an interactive part of a Fortran program

Although many of you will have a decent idea of what I'm aiming at, just from reading the title -- allow me a simple introduction still.
I have a Fortran program - it consists of a program, some internal subroutines, 7 modules with its own procedures, and ... uhmm, that's it.
Without going into much detail, for I don't think it's necessary at this point, what would be the easiest way to use MATLAB's plotting features (mainly plot(x,y) with some customizations) as an interactive part of my program ? For now I'm using some of my own custom plotting routines (based on HPGL and Calcomp's routines), but just as part of an exercise on my part, I'd like to see where this could go and how would it work (is it even possible what I'm suggesting?). Also, how much effort would it take on my part ?
I know this subject has been rather extensively described in many "tutorials" on the net, but for some reason I have trouble finding the really simple yet illustrative introductory ones. So if anyone can post an example or two, simple ones, I'd be really grateful. Or just take me by the hand and guide me through one working example.
platform: IVF 11.something :) on Win XP SP2, Matlab 2008b
The easiest way would be to have your Fortran program write to file, and have your Matlab program read those files for the information you want to plot. I do most of my number-crunching on Linux, so I'm not entirely sure how Windows handles one process writing a file and another reading it at the same time.
That's a bit of a kludge though, so you might want to think about using Matlab to call the Fortran program (or parts of it) and get data directly for plotting. In this case you'll want to investigate Creating Fortran MEX Files in the Matlab documentation. This is relatively straightforward to do and would serve your needs if you were happy to use Matlab to drive the process and Fortran to act as a compute service. I'd look in the examples distributed with Matlab for simple Fortran MEX files.
Finally, you could call Matlab from your Fortran program, search the documentation for Calling the Matlab Engine. It's a little more difficult for me to see how this might fit your needs, and it's not something I'm terribly familiar with.
If you post again with more detail I may be able to provide more specific tips, but you should probably start rolling your sleeves up and diving in to MEX files.
Continuing the discussion of DISLIN as a solution, with an answer that won't fit into a comment...
#M. S. B. - hello. I apologize for writing in your answer, but these comments are much too short, and answering a question in the form of an answer with an answer is ... anyway ...
There is the Quick Plot feature of DISLIN -- routine QPLOT needs only three arguments to plot a curve: X array, Y array and number N. See Chapter 16 of the manual. Plus only several additional calls to select output device and label the axes. I haven't used this, so I don't know how good the auto-scaling is.
Yes, I know of Quickplot, and it's related routines, but it is too fixed for my needs (cannot change anything), and yes, it's autoscaling is somewhat quircky. Also, too big margins inside the graf.
Or if you want to use the power of GRAF to setup your graph box, there is subroutine GAXPAR to automatically generate recommended values. -2 as the first argument to LABDIG automatically determines the number of digits in tick-mark labels.
Have you tried the routines?
Sorry, I cannot find the GAXPAR routine you're reffering to in dislin's index. Are you sure it is called exactly like that ?
Reply by M.S.B.: Yes, I am sure about the spelling of GAXPAR. It is the last routine in Chapter 4 of the DISLIN 9.5 PDF manual. Perhaps it is a new routine? Also there is another path to automatic scaling: SETSCL -- see Chapter 6.
So far, what I've been doing (apart from some "duck tape" solutions) is
use dislin; implicit none
real, dimension(5) :: &
x = [.5, 2., 3., 4., 5.], &
y = [10., 22., 34., 43., 15.]
real :: xa, xe, xor, xstp, &
ya, ye, yor, ystp
call setpag('da4p'); call metafl('xwin');
call disini(); call winkey('return');
call setscl(x,size(x),'x');
call setscl(y,size(y),'y')
call axslen(1680,2376) !(8/10)*2100 and 2970, respectively
call setgrf('name','name','line','line')
call incmrk(1); call hsymbl(3);
call graf(xa, xe, xor, xstp, ya, ye, yor, ystp); call curve(x,y,size(x))
call disfin()
end
which will put the extreme values right on the axis. Do you know perhaps how could I go to have one "major tick margin" on the outside, as to put some area between the curve and the axis (while still keeping setscl's effects) ?
Even if you don't like the built-in auto-scaling, if you are already using DISLIN, rolling your own auto-scaling will be easier than calling Fortran from MATLAB. You can use the Fortran intrinsic functions minval and maxval to find the smallest and largest values in the data, than write a subroutine to round outwards to "nice" round values. Similarly, a subroutine to decide on the tick-mark spacing.
This is actually not so easy to accomplish (and ideas to prove me wrong will be gladly appreciated). Or should I say, it is easy if you know the rough range in which your values will lie. But if you don't, and you don't know
whether your values will lie in the range of 13-34 or in the 1330-3440, then ...
... if I'm on the wrong track completely here, please, explain if you ment something different. My english is somewhat lacking, so I can only hope the above is understandable.
Inside a subroutine to determine round graph start/end values, you could scale the actual min/max values to always be between 1 and 10, then have a table to pick nice round values, then unscale back to the correct range.
--
Dump Matlab because its proprietary, expensive, bloated/slow and codes are not easy to parallelize.
What you should do is use something on the lines of DISLIN, PLplot, GINO, gnuplotfortran etc.