Add ddmmm to the year column

Hi,
I have a matrix "c" has year column and I need to add "31May" in front of each year in the column. So, the resulting array should look like below: (first two rows are shown). Note that years are not always in a sequence.
How can I achieve this in MATLAB.?
Thanks in advance.
31May1966 5261.9
31May1967 6003.5

 Accepted Answer

out = [arrayfun(@(x)sprintf('31May%d',x),c(:,1),'un',0),num2cell(c(:,2))];

4 Comments

Damith
Damith on 23 Dec 2015
Edited: Damith on 23 Dec 2015
Forgot to mention earlier that is there a way storing into a double array or something since I am planning to use "csvwrite" function. I want the 1st column as ddmmmyyyy without any commas or space. (eg. 31May1966)
See my code below:
filePattern = fullfile(myFolder, '*.csv');
d = dir(filePattern);
fmt='%4f %6.1f %*[^\n]';
for i=1:length(d)
fid = fopen(fullfile(myFolder,d(i).name));
c=cell2mat(textscan(fid,fmt,'collectoutput',true,'headerlines',0,'delimiter',','));
fid=fclose(fid);
%out = [arrayfun(@(x)sprintf('31May%d',x),c(:,1),'un',0),num2cell(c(:,2))];
stn=strtok(d(i).name,'_')
csvwrite([stn '.csv'],out)
clear out
end
csvwrite is not suitable anything other than numeric arrays; dlmwrite() is the same. You can use xlswrite() with a .csv file extension, or you can use one of the techniques shown here
xlswrite function worked here. But, when I open the file, the 1st column has dd-mmm-yyyy. There is a "-". Is there a way to specify the format of the 1st column?

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!