How to define a new variable in a table, Part 2
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Suppose I have a table 'test' such as
month
-------
January
February
...
I now want to define a string variable 'season' such that
'season'='winter' if 'month' is 'January', 'February', or 'March'
etc. The output would be
month season
---- ---
January winter
February winter
...
Please advise.
2 commentaires
Rik
le 18 Juin 2020
I don't work with tables myself, but a rowfun would probably do the trick. Just write a function that looks up the season for every moth (use ismember).
Note that this swaps for people in the southern hemisphere.
Réponse acceptée
Ameer Hamza
le 18 Juin 2020
Try this
T = % you table
Months = {'January', 'February', 'March', 'April', 'May', 'June', ...
'July', 'August', 'September', 'October', 'November', 'December'}.';
Mnth2Season = [1 1 1 2 2 2 3 3 3 4 4 4].'; % for example
Seasons = {'Winter', 'Spring', 'Summer', 'Autumn'}.';
[~, idx] = ismember(T.Months, Months);
T.Season = Seasons(Mnth2Season(idx));
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Cell Arrays 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!