收藏到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!
8 Dec 2022
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.
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.
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?
29 Oct 2022
Python Tip: Get Filename, Extension, Parent Folder, File Existence, and More
收藏到CSDN网摘
From time to time, a very common task might be to get the file name, extension, parent folder, or checking the exitance of a file or file list. In Python, this can be done by the in-built os.path library. Here is how!
From time to time, a very common task might be to get the file name, extension, parent folder, or checking the exitance of a file or file list. In Python, this can be done by the in-built os.path library. Here is how!
27 Oct 2022
Python Tip: Get Variable Name in Python (type of reflection?)
收藏到CSDN网摘
Sometimes, you may want to get the variable name itself as a string while writing Python code. For instance, if you are going to write 'a = 2', is there a convenient way of getting the variable name '"a"' by calling a function using 'a' as the parameter? The short answer is YES.
Sometimes, you may want to get the variable name itself as a string while writing Python code. For instance, if you are going to write 'a = 2', is there a convenient way of getting the variable name '"a"' by calling a function using 'a' as the parameter? The short answer is YES.
21 Oct 2022
Get Date and Time String in Python
收藏到CSDN网摘
It's a very common task that you may need the current date and time as a string, which can then be assigned as a timestamp for logging, intermidiate input deck generation, etc. In Python, there are several modules available to achieve this, such as date, time, datetime - well, it looks redudent here! The easierest way might be using the time module.
It's a very common task that you may need the current date and time as a string, which can then be assigned as a timestamp for logging, intermidiate input deck generation, etc. In Python, there are several modules available to achieve this, such as date, time, datetime - well, it looks redudent here! The easierest way might be using the time module.
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.
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.
27 Jun 2022
Subscribe to:
Posts
(
Atom
)