Showing posts with label matlab. Show all posts
Showing posts with label matlab. Show all posts

24 Mar 2023

Matlab: Terminate Execution by emulating CTRL+C

收藏到CSDN网摘

This is a really usful trick for GUI development in Matlab. Although Matlab has some advanced methods such as Future object to run the process in background. But a simple method to mimic what the user does by pressing CTRL+C would be nice to have!

7 Mar 2023

Matlab: convert time duration to string

收藏到CSDN网摘

Matlab has its own tic/toc function to measure the running time a peice of code, which is very convenient and widely used by developers. The duration() function along with hours(), minutes(), and seconds() is another way of creating time durations explicitly. All of these duration types need to be converted to string format for displaying purpose. How can/should we convert them properly?

8 Dec 2022

Find All Opened Matlab Figures

收藏到CSDN网摘

Sometimes, Matlab is really good at plotting data for analysis or image processing. The simple figure() function can be used to create a new figure to plot on. Otherwise, it will plot directly on the last existing (or the current figure, which can be accessed via gcf function) figure. Moreover, the existing plotted data would be overwritten if you forgot to turn 'hold on' to preserve the current plots. So it's very common to always call figure() to get a new convas before plotting. However, this leads to another problem that you may get massive number of figures opened after a while using Matlab. I call it as Figure Explosion. To get rid of all open figures, one can simply type 'close all'. However, what if there are some figures you'd like to keep but you just cannot find them manually. How can we close some figures selectively based on there properties, e.g. names maybe? Here it is!

11 Nov 2022

Generalised Conversion from Any Data Type to String in Matlab

收藏到CSDN网摘

For logging and debugging purpose in Matlab, some parameter values need to be written either to the command window or an external file. To display in the command window, we have two options - disp() and fprintf(). The disp() is very convenient and works with all data types. However, we must stick to fprintf() if more flexible formats are required, such as %f, %g, %s, %d, etc. But it means treating different data types differently. 

Is there a simple and generalised way of doing this? Or is Matlab clever enough to convert any data type to a proper string format for displaying? The answer is YES!

Get DateTime as a Formatted String in Matlab

收藏到CSDN网摘

A very common task would be obtain the current timestamp while writing code in any programming languages, especially for logging and debugging purpose. Almost all the languages provide support for retrieving the current date and time using either built-in or 3rd party libs. 

Matlab has some built-in formats already. All we need to do is to pass the proper format number to its 'datestr()' function. To obtain the current time and date, 'now' can be passed to the 'datestr()' function as the first parameter. The 2nd parameter is for specifying the format. Someone may write code like "datestr(now, 'yyyy-mm-dd HH:MM:SS')" to get a timestamp such as 2022-11-10 00:00:42. However, apart from explicitely writing out the format string, Matlab also supports specifying the format using a single number - "datestr(now, 31)" will give you the same result. How about other formats?

30 Sept 2022

Change Matlab Command Window's Text Colour

收藏到CSDN网摘

Matlab has provided some coloured text features already. For instance, the default 'disp' or 'fprintf' will print text in balck colour, while 'warning' displays text in orange and 'error' displays text in red (yes, error also terminates your script). But someone may want more flexibilities over these default one. Fortunately, there are some work around available for this issue.

28 Apr 2021

How to test if a point is inside a sector?

收藏到CSDN网摘

If we have a sector (partial circle) defined by the centre C, radius R, starting angle point S, and ending angle point E. How can we test if a given point P is inside or outside the sector? The idea is to define 2 function: the one is to test if one vector can be obtained by rotating from another vector clockwise (or anti-clockwise if needed); the other is to test if the distance from the point to the centre is less than or equal to the radius. Then, the problem becomes 3 tests: (1) clockwise rotation from starting vector to the point; (2) anti-clockwise rotation from the ending vector to the point; and (3) the distance from P to C is less than R.

How to test if a point is inside of a triangle?

收藏到CSDN网摘

To determine if a point P is inside or outside a given triangle defined by 3 vertices P1, P2, and P3. The edges can be calculated first as 3 vectors: P12 = P1-P2; P23 = P2-P3; P31 = P3-P1. Then, its a simple task.

14 Jun 2018

Get Matlab Version

收藏到CSDN网摘

Matlab provides loads of useful functions for scientific computing. However, the functions might be added or deprecated from version to version. To write more robust codes, it's always a good practice to deal with different cases. An if...else... clause would be enough for this purpose.

9 Feb 2018

Get the latest release folder from a directory

收藏到CSDN网摘
Sometimes, we might need to find the latest release folder from a software repository according to the time stamp of the folder property. This could be done by extracting all the time stamps and sort them accordingly. MATLAB provides several function to do this nicely.

16 Jan 2018

Scroll to the last line of text edit in Matlab

收藏到CSDN网摘
Matlab's edit UIControl provide some flexibilities for displaying information, while setting the Max-Min>1 it supports multiple line. However, the caret is alwasy set to 0 position in the first line which is very annoy while you would like to show dynamic updated information to the user. To implement this capability, the underlying Java class has to be digged out. Yair M. Altman's findjobj.m function works perfectly to obtain those kind of information. Then, all we need to do is providing the Matlab UIControl's handle and set caret to the document length.

15 Jun 2017

Finding Intersection Point(s) of Two Circles

收藏到CSDN网摘
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].

4 May 2017

draw rectangle, polygon and circle in Matlab

收藏到CSDN网摘
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().

Pros:
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.

Cons:
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

收藏到CSDN网摘
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!

1 Aug 2013

matlab的annotation必须用uniform坐标

收藏到CSDN网摘


经常碰到matlab图像需要标注的情况,annotation可以很方便的加入线,箭头,方框等标注信息,但是箭头等位置需要用归一化的坐标来表示,如何转换呢?

1 May 2013

matlab制作分形图

收藏到CSDN网摘



分形是一个很常见的数学问题,一般方法是递归,但是matlab的递归速度比较低下.8层的分形图制作递归的话,大概需要1.9s左右.
下面的代码是迭代方法.

10 Apr 2013

matlab求内外接圆与最小包围矩阵

收藏到CSDN网摘
matlab图像处理功能非常强大,regionprops只可以求水平包围框,下面的程序可以实现边缘的内外接圆与最小包围框(不一定是水平的)和凸包.