22 Oct 2023

Customise Mouse Keys (including side keys)

收藏到CSDN网摘

I purchased a mouse some time ago from Amazon. It features 2 additional side buttons in addition to the typical left-right-middle layout. I was pleasantly surprised to find that these side buttons functioned seamlessly while browsing webpages, serving as the backward/forward navigation controls in the browsing history. This convenience sufficed for most of my needs.

However, I required the side buttons to emulate the LEFT/RIGHT or UP/DOWN arrow keys to ensure smooth navigation within my HTML slide deck for teaching. This mouse is budget-friendly, and unfortunately, the manufacturer does not provide any driver or software support for customisation.

Fortunately, I came across a free software solution known as X-Mouse Button Control, which expertly resolved my issues. I have successfully tested it on a Windows 11 x64 system, and it has been reported to work on Windows 7 and Windows 10 as well. If you encounter a similar issue, it's certainly worth trying out this software.

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?