splitting cell array consists of string and numeric characters
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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?
Accepted Answer
Use replace
S = {...}; % your n*1 cell array
S = arrayfun(@(i)replace(S{i},'*',''),1:1:numel(S),'uniform',false);
9 Comments
And if you want to transform S later to datetime then use'
DT = arrayfun(@(i)datetime(str2num(S{i})),(1:1:numel(S))')
sermet OGUTCU
on 26 Aug 2021
Edited: sermet OGUTCU
on 26 Aug 2021
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?
Wan Ji
on 26 Aug 2021
S = {...}; % your n*1 cell array
S = arrayfun(@(i)num2str(str2num(replace(S{i},'*',''))),1:1:numel(S),'uniform',false);
Wan Ji
on 26 Aug 2021
Or you can do
S = {...}; % your n*1 cell array
S = arrayfun(@(i)replace(S{i},'* ',''),1:1:numel(S),'uniform',false);
sermet OGUTCU
on 26 Aug 2021
Edited: sermet OGUTCU
on 26 Aug 2021
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.
Wan Ji
on 26 Aug 2021
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));
Wan Ji
on 26 Aug 2021
Wan Ji
on 26 Aug 2021
You may see n*6 cell is not so good at all compared with DT or DV
sermet OGUTCU
on 26 Aug 2021
Thank you very much for the solution.
More Answers (0)
Categories
Find more on Cell Arrays in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)