22 Dec 2017

Maxmise plot in matplotlib


To make the plot maxmised, different commands need to be used according to the current backends used by matplotlib:

Save plot without displaying in Matplotlib

By using Matplotlib, data can be easily plotted in the same way as in Matlab. But sometimes you might need to plot thousands of plots in a bug loop and save those intermediate results. The interactive display could be an issue that makes your machine not only slow but also useless. Someone suggests 'plt.ioff()' to turn off interactive mode. It may work in some cases (according to the feedbacks on Stackoverflow). For me, no magic happened. After searching the internet, the most straightforward solution would be...

15 Jun 2017

Finding Intersection Point(s) of Two Circles

This is a very common problem of finding the intersection point(s) of two circles. Visually, it is easy to define the three cases: no intersection, touching (single intersection point), and two intersection points. Analytically, mathematical calculations are required to work out the exact intersection position(s) (if any) by using the equations of the two circles. A more detailed explanation could be find from HERE and HERE.

Below, it's my implementation in Matlab. The inputs are c1 and c2, which are both vectors representing a circle in the format of [x,y,d]. The output is the point list matrix sorted using y coordinates. If there is no intersection, it is an empty matrix. For touching case, it is a single-row matrix (or vector) in the format of [x,y]. Otherwise, it is a 2*2 matrix forming by the coordinates of the two intersection points: [x1,y1;x2,y2].

5 Jun 2017

Excel Spreadsheet Format

MS's flagship office suits are widely used everywhere, even by the calculation-extensive engineering world. There are always someone that like Excel much more than the others. From a software engineer's point of view, it might be more straightforward to extract information directly from the file itself. MS provides the COM objects. The .net platform also enables a more generic but user-friendly way of manipulating office documents including docx and xlsx files. If you'd like to stay ways from COM or .Net platform, you'd better to find another way of doing it. This article illustrates how. I will focus on excel file rather than docx. But the concept should be similar, unless you show me it's not the case :-)

4 May 2017

draw rectangle, polygon and circle in Matlab

To draw these shapes in Matlab, different ways could be used to achieve this. Write your own function to use plot(), or use rectangle() or use patch().

The rectangle() can be used to draw ellipse, circle, round-corner rectangle as well if you specify the 'Curvature' parameter.

The plot() and patch() uses the same input as (x,y) list, which means x, and y are vectors of corresponding coordinates. You can use these two function draw polygons with any number of points.

rectangle() uses input as [x,y,w,h] rather than (x,y) list. rectangle() can only accept input as a 4 elements vector. It requires calling of daspect([1,1,1]) to make the equality along each axis. However, it is very handy if you are drawing proper rectangle and circle.

15 Feb 2017

Close all messageboxes in Matlab

In matlab, user interaction could be easily achieved by using 'msgbox()' function. The format of calling the 'msgbox()' is: msgbox(msg,title,style,mode). It is also very useful to modify the 'style' parameters to customise the appearance of the message box. The build-in parameters are:
'none' - the default one if you didn't provide any parameter
'warn' - warning dialogue box
'help' - help dialogue box
'error' - error dialogue box

The last parameter 'mode' can be selected among 'modal', 'nonmodal', and 'replace'. Some examples are shown in the picture below.

But, how to close these dialogue boxes? You might though: are you kidding me? click on the button - that's as easy as breath. Yes, that's exactly what everyone will do for sure. Seriously, think about if you have hundreds, even thousands of these little evils created in a loop? - yeah, I know it's a bad idea to create these GUI elements in your code if you are doing something computational extensive. But I saw someone did this before. It might be a legacy code you are maintaining which never come across this warning before. Or you just started from a small dataset and need to debug in this way while you progresses gradually. It's even worse if you created these dialogue boxes using 'modal' mode - it means you can do nothing before closing them. That's the real nightmare!

Let's try to find a way of dealing with this!