splitting cell array consists of string and numeric characters
显示 更早的评论
I have a cell array (n x 1). Each cell looks something like this:
* 2021 3 29 0 0 0.00000000
I need to remove * from each cell and split the cell array into 6 columns consist of double values. For example;
2021 3 29 0 0 0
2021 3 30 0 0 0
.
.
.
How I can do that?
采纳的回答
Use replace
S = {...}; % your n*1 cell array
S = arrayfun(@(i)replace(S{i},'*',''),1:1:numel(S),'uniform',false);
9 个评论
And if you want to transform S later to datetime then use'
DT = arrayfun(@(i)datetime(str2num(S{i})),(1:1:numel(S))')
It removed the * and produced 1 x n cell array. But I need to split the "2021 3 29 0 0 0.00000000" into 6 columns. Could you show the next steps to do this?
S = {...}; % your n*1 cell array
S = arrayfun(@(i)num2str(str2num(replace(S{i},'*',''))),1:1:numel(S),'uniform',false);
Or you can do
S = {...}; % your n*1 cell array
S = arrayfun(@(i)replace(S{i},'* ',''),1:1:numel(S),'uniform',false);
Dear @Wan, I still get 1 x n cell array instead of n x 6 double array consists of 2021 3 29 0 0 0.00000000 double values. I edited my question to be more specific.
S = arrayfun(@(i)num2str(str2num(replace(S{i},'*',''))),1:1:numel(S),'uniform',false);
DT = arrayfun(@(i)datetime(str2num(S{i})),(1:1:numel(S))');
DV = datevec(DT);
YourCelln6 = mat2cell(DV,ones(size(DV,1),1),ones(size(DV,2),1));
You may see n*6 cell is not so good at all compared with DT or DV
Thank you very much for the solution.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Cell Arrays 的更多信息
标签
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
