Effacer les filtres
Effacer les filtres

How to convert number to string vector

2 vues (au cours des 30 derniers jours)
Danny
Danny le 25 Oct 2014
Commenté : Danny le 25 Oct 2014
I have three vectors representing years, months, and days:
a = [2014;2014];
b = [10;11];
c = [25; 24];
and at the very least I want to put them in a string vector such as:
Vector = ['2014_10_25' ; '2014_11_24']
I am unsure how to do this conversion. The best case would be an output of:
Vector = ['2014_Oct_25' ; '2014_Nov_24']
but my goal is to convert the year, month, and day numerical vectors into one string vector.

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 25 Oct 2014
Modifié(e) : Azzi Abdelmalek le 25 Oct 2014
a = [2014;2014];
b = [10;11];
c = [25; 24];
x=[a b c zeros(numel(a),3) ]
out=datestr(x,'yyyy_mmm_dd')

Plus de réponses (1)

dpb
dpb le 25 Oct 2014
Trivial...
>> [a,b,c]
ans =
2014 10 25
2014 11 24
>> datestr(datenum(a,b,c)) % default format
ans =
25-Oct-2014
24-Nov-2014
>> datestr(datenum(a,b,c),'yyyy-mmm-dd') % your requested format
ans =
2014-Oct-25
2014-Nov-24
>>
See
doc datenum % and friends for details
As hint on how to find these things while learning Matlab,
>> lookfor date % returns in part
collectdata - Given cell array of nx3 per cell of date, stockID, return as
doc_datacursormode - Plots graph and sets up a custom data tip update function
myadddate - ADDDATE - version vectorized...
sunny - Given cell array of nx3 per cell of date, stockID, return as
dateTickPicker - Returns ticks for "best" scale.
datetickstr - Returns the date string associated with the values input. Any values of
invalidateaxis - Invalidate an axis to recompute limits and ticks
convertSpreadsheetDates - Convert cells in a spreadsheet to MATLAB datenum format
...
addtodate - Add a quantity to a date field.
clock - Current date and time as date vector.
date - Current date as date string.
datenum - Serial date number.
datestr - String representation of date.
datetick - Date formatted tick labels.
datevec - Date components.
now - Current date and time as date number.
...
  1 commentaire
Danny
Danny le 25 Oct 2014
Thank you for the very helpful answer

Connectez-vous pour commenter.

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by