How to replace a string in a table
26 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
更多回答(1 个)
Deepak Gupta
2020-6-18
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 个评论
Mara
2020-6-18
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operators and Elementary Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!