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

2/24/2011

A first attempt to regular expression

Suppose we have a Time sting (h)h:mm:dd. in 12 hr format. This will cause some trouble when attempting to construct a column vector in Matlab because the difference of the length. It is of course very easy to address this problem by testing whether the length of the string is 8 or 7 and pad blank at the front.
Another way is to use regular expression.
Suppose we have Time T1 = '1:30:20' and T2 = '12:20:40'
regexprep(T1,'^\w:',...
'${strcat(num2str(str2num($&(1))+12),'':'')}')
yields 13:30:30
evaluating on T1 yields 12:20:40


2/22/2011

Output numeric matrix to csv with a header


Courtesy of http://www.squaregoldfish.co.uk/2010/04/11/writing-csv-files-with-headers-in-matlab/

header=['test 1, test 2, test 3'];
outid = fopen(‘out.csv’, ‘w+’);
fprintf(outid, ‘%s’, header);
fclose(outid);
dlmwrite (‘out.csv’,csvData,’roffset’,1,’-append’)