how to add zeros in fronts of any number in matrix

dten = datenum([1998 1 1 ;2002 12 31]);
[M] = datevec(dten(1):dten(2));
years = M(:,1);
months = M(:,2);
days = M(:,3);
i want to add zeros in front of 1, 2, 3, 4, 5, 6, 7, 8, 9 like this 01, 02, 03, 04, 05, 06, 07, 08, 09 in months and days matrix. how will do this. please help me. thank you in advance.

 採用された回答

Konstantinos Sofos
Konstantinos Sofos 2015 年 3 月 22 日
編集済み: Konstantinos Sofos 2015 年 3 月 22 日

0 投票

Asking for this probably you do not want anymore your years, months, days vectors to be numbers but chars. Because you know from basic maths that '01' doesn't exist as type of number.
myCellMonth = cellfun(@(x) num2str(x),num2cell(months),'UniformOutput', false);
idx = cellfun(@(x) numel(x),myCellMonth);
idx = idx==1; % we want to add a leading '0' only in front of 1,2,3,...9
myCellMonth(idx) = cellfun(@(x) strcat('0', x),myCellMonth(idx),'UniformOutput', false)
In the same logic you could reform days and years
Regards

5 件のコメント

Konstantinos Sofos
Konstantinos Sofos 2015 年 3 月 22 日
編集済み: Konstantinos Sofos 2015 年 3 月 22 日
After your comment you could use the above as
filename1 = ['3B42_daily.',myCellYears{i},'.',myCellMonth{i},'.',myCellDay{i},'.7.SUB.nc']
And as an example if i = 1
filename1 = ['3B42_daily.',myCellyears{1},'.',myCellMonth{1},'.',myCelldays{1},'.7.SUB.nc']
will give you as result
filename1 =
3B42_daily.1998.01.01.7.SUB.nc
devendra
devendra 2015 年 3 月 22 日
編集済み: devendra 2015 年 3 月 22 日
thank you so much Konstantinos Sofos this is helping alot but can we remove ' ' (inverted commas) from this because this is giving this type of output.
'01'
'01'
'01'
Konstantinos Sofos
Konstantinos Sofos 2015 年 3 月 22 日
dten = datenum([1998 1 1 ;2002 12 31]);
[M] = datevec(dten(1):dten(2));
years = M(:,1);
months = M(:,2);
days = M(:,3);
%DAYS
myCellDay = cellfun(@(x) num2str(x),num2cell(days),'UniformOutput', false);
idx = cellfun(@(x) numel(x),myCellDay);
idx = idx==1;
myCellDay(idx) = cellfun(@(x) strcat('0', x),myCellDay(idx),'UniformOutput', false);
%MONTHS
myCellMonth = cellfun(@(x) num2str(x),num2cell(months),'UniformOutput', false);
idx = cellfun(@(x) numel(x),myCellMonth);
idx = idx==1; % we want to add a leading '0' only in front of 1,2,3,...9
myCellMonth(idx) = cellfun(@(x) strcat('0', x),myCellMonth(idx),'UniformOutput', false);
% YEARS
myCellYears = cellfun(@(x) num2str(x),num2cell(years),'UniformOutput', false);
idx = cellfun(@(x) numel(x),myCellYears);
idx = idx==1;
myCellYears(idx) = cellfun(@(x) strcat('0', x),myCellYears(idx),'UniformOutput', false);
for i = 1:numel(M(:,1))
filename = ['3B42_daily.',myCellYears{i},'.',myCellMonth{i},'.',myCellDay{i},'.7.SUB.nc']
end
Konstantinos Sofos
Konstantinos Sofos 2015 年 3 月 22 日
for the ' ' (inverted commas) use {} instead of () to read the context of a char cell array as i showed above. Hope that this helps.
devendra
devendra 2015 年 3 月 22 日
thank you so much Konstantinos Sofos for helping me.

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2015 年 3 月 22 日

1 投票

sprintf and also num2str can use the '%02d' format:
myCellMonth = cellfun(@(x) num2str(x, '%02d'), num2cell(months),'UniformOutput', false);
This inserts a leading zero, when the number has less than 2 digits. Instead of preparing cell strings, you can use sprintf directly:
for i = 1:numel(M(:,1))
filename = sprintf('3B42_daily.%04d.%02d.%02d.7.SUB.nc', year(i), month(i), day(i));
...
end

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

質問済み:

2015 年 3 月 22 日

回答済み:

Jan
2015 年 3 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by