Thursday, October 11, 2012

How to check if the input is a number (Matlab)

If you are writing a code where the user is asked to give a number, it can be a good idea to check whether or not the inserted value was really a number instead of something else like a letter or some strange character (%, / _ , etc).

The easiest way to do it is asking for a string input and then attempting (via str2num) to make the conversion string to number. If the conversion fails, then the input wasn't a number!

Here a very simple code that does the job.


function [varargout]=CheckIfNumber(s)
% s is a string
% varargout is the converted number or row vector


if (isempty(num)==0)




Wednesday, October 3, 2012

How to get a timestamp (date + time) in Matlab

To complete my  Matlab interface for data fitting, I  needed a Timestamp function, namely a function combining date and time into a single string.

This can be useful when you want to save files having the same base "name" (e.g. foo-x11.dat, foo-x12.dat,  etc) but created at different times.

In Matlab the timestamp is obtained with the command

>>datestr(clock, 0)

which output is (at the time of this post!!!)

10-nov-2012 14:10:20

Unfortunately this string cannot be used into a filename due to the presence of colons.
In addition I would like to have a string starting with "year-month-day" because it is a much more convenient  way to name files created at different times.

My version of TimeStamp that produces a string in the format :


as for example


Here is the code! Enjoy!
And keep following this blog and my other one.  It's totally worth it!


function [s]=TimeStamp
% time stamp in the format
% year-month-day-hours-h-minutes-m-second-s

% Get current time as date vector


% convert date vector into the string: year-month-day
yyyy_mm_dd=datestr(t, 29);

% convert date vector into the string: day-month-year[blank]hours:minutes:seconds
s=datestr(t, 0);

% get index of the blank
index=find(s==' ');

% remove the string that is before the blank

% get the indices of the colons ":"

%change the first colon into "h" and the second one into "m". Add an "s" at the end of the

h_m_s=strcat(  s(1:(ii(1)-1)), 'h', s( (ii(1)+1): (ii(2)-1)), 'm', s( (ii(2)+1):end), 's');

% concate the two strings
s=strcat(yyyy_mm_dd, '-', h_m_s);

Alternatively, you can use the very short code provided by the first commenter :)
In this case:

function s=TimeStamp

Or the code built on the suggestion provided by the another commenter :)

function s=TimeStamp

tmp = strrep(datestr(clock), ':' , '-' );
s= strrep(tmp, ' ' , '-' );
s=strcat(s, 's');

Tuesday, October 2, 2012

Make Matlab listing ONLY subfolders cointained into a folder

From the Matlab Command Window when you type "dir" you get a list of files and folders contained in your working directory.
The question is: how can you get ONLY the folders?

Here is the solution from stackoverflow.

In the following function the output "nameFolds" is a cell variable type!!!


function nameFolds=ListSubfolders(pathFolder)

d = dir(pathFolder);
isub = [d(:).isdir];
nameFolds = {d(isub).name}';
nameFolds(ismember(nameFolds,{'.','..'})) = [];