How to replace a string in a table

9 vues (au cours des 30 derniers jours)
alpedhuez
alpedhuez le 18 Juin 2020
Modifié(e) : alpedhuez le 18 Juin 2020
I have a table test that says
month
--
'January'
'JANUARY'
I want to replace 'JANUARY' by 'January':
month
--
'January'
'January'
I tried strcmp but I got an error message "conversion to cell from char is not possible." Please advise.

Réponse acceptée

Mara
Mara le 18 Juin 2020
Modifié(e) : Mara le 18 Juin 2020
test = table();
test.month = 'JANUARY';
test.month = lower(test.month); %writes it in lower case letters
or you can as well just replace it by your desired input
test.month = 'january';
P.S. You do not have a string but a character (char)
  5 commentaires
Mara
Mara le 18 Juin 2020
Modifié(e) : Mara le 18 Juin 2020
then you have to specify that you want every row:
test.month(:,2:end) = lower(test.month(:,2:end));
alpedhuez
alpedhuez le 18 Juin 2020
Modifié(e) : alpedhuez le 18 Juin 2020
IQ=IQ+1; % Thank you.

Connectez-vous pour commenter.

Plus de réponses (1)

Deepak Gupta
Deepak Gupta le 18 Juin 2020
I am providing a solution, i suspect it's not the ideal solution but it should work.
test = table();
test.month{1} = 'JANUARY';
test.month{2} = 'January';
for index = 1:length(test.month)
if(test.month{index}=='JANUARY')
test.month{index} = 'January';
end
end
  1 commentaire
Mara
Mara le 18 Juin 2020
It depends on whether you have a cell array containing characters or characters solely. From the previous error message that occured ("conversion to cell from char is not possible.") , I concluded, that it is stored as characters only. In which case it does not let you index with curly braces
this worked for me:
test = table();
test.month = {'JANUARY'; 'FEBRUARY'; 'MARCH'}; %cell
test.month = char(test.month); %conversion to character array
test.month(:,2:8) = lower(test.month(:,2:8)); %indexing with normal braces

Connectez-vous pour commenter.

Catégories

En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange

Tags

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by