How can I split the data in cells

for i = 1:30
for j = 1:7
ce461_hw3_partc_data(i,j) = ce461_hw3_partb_cell_sorted (i,j);
end
end
ce461_hw3_partc_data_names = ce461_hw3_partc_data(:,1); %To obtain the 30x7 cell data%
ce461_hw3_partc_newstring = split(ce461_hw3_partc_data_names); %To obtain split version of each datum but didn't work%
% for row = 1:30
% ce461_hw3_partc_data_string (row,1) = ce461_hw3_partc_data(row,1);
% wordcount= numel(strsplit(ce461_hw3_partc_data_string (row,1)));
% if wordcount>=4
% outstring = instring;
% outstring (instring == '_')= ' '; %https://uk.mathworks.com/matlabcentral/answers/854770-using-a-loop-to-replace-spaces-for-underscore%
%
% end
% end
First of all, I am not good at MATLAB at the moment and I am trying my best, since it is my homework, I am trying to figure out what to do in order to split the data given to us. Such as, the data given is consisting of F1 drivers and their countries 'United Kingdom Lewis Hamilton' and 'Germany Sebastian Vettel'. The expected work for me is to obtain a column consisting of 'Lewis_Hamilton'. How can I achieve what I want to achieve, any help could be appreciated.
To be more understandable, I need to obtain 'Lewis_Hamilton' from 'United Kingdom Lewis Hamilton'.

7 Comments

Can you please attach your data using the paper clip button?
By deleting your question after you got answers for it might make it so that no one will answer any future questions from you. Just saying.....
iii
iii on 2 Jan 2023
Edited: iii on 2 Jan 2023
Yeah, you are right, but since that was a homework, it turned out to be the fact that I am not allowed to share any part of the homework, so I had to remove the question but I can not do that because it has already been answered. I got a warning from the teacher that if it is possible, I should definitely remove the question, if it is not possible, just revise the question that no other student can easily reach out the answer. That's why I have removed it. But, I am now going to revise the question.
Matt J
Matt J on 2 Jan 2023
Edited: Matt J on 2 Jan 2023
That's why I have removed it. But, I am not going to revise the question.
Well, the forum moderators are going to restore your original question eventually, so leaving it as is will not fulfill your teacher's wishes. Your only option is to compose a revised question for which the answers posted below are still valid.
iii
iii on 2 Jan 2023
Edited: iii on 2 Jan 2023
A little misunderstanding about the condition that I have stated above. It should be "I am now going to revise the question" instead of "I am not going to revise the question". Sorry guys. There was a typo that I incidentally have done, but now I have revised the question. I am not the type of selfish guy that when I get the answers, I close the question.
Original question posted by Serhat Ardic, retrieved from Google Cache:
"How can I split the data in cells"
for i = 1:30
for j = 1:7
ce461_hw3_partc_data(i,j) = ce461_hw3_partb_cell_sorted (i,j);
end
end
ce461_hw3_partc_data_names = ce461_hw3_partc_data(:,1); %To obtain the 30x7 cell data%
ce461_hw3_partc_newstring = split(ce461_hw3_partc_data_names); %To obtain split version of each datum but didn't work%
% for row = 1:30
% ce461_hw3_partc_data_string (row,1) = ce461_hw3_partc_data(row,1);
% wordcount= numel(strsplit(ce461_hw3_partc_data_string (row,1)));
% if wordcount>=4
% outstring = instring;
% outstring (instring == '_')= ' '; %https://uk.mathworks.com/matlabcentral/answers/854770-using-a-loop-to-replace-spaces-for-underscore%
%
% end
% end
First of all, I am not good at MATLAB at the moment and I am trying my best, since it is my homework, I am trying to figure out what to do in order to split the data given to us. Such as, the data given is consisting of F1 drivers and their countries 'United Kingdom Lewis Hamilton' and 'Germany Sebastian Vettel'. The expected work for me is to obtain a column consisting of 'Lewis_Hamilton'. How can I achieve what I want to achieve, any help could be appreciated.
To be more understandable, I need to obtain 'Lewis_Hamilton' from 'United Kingdom Lewis Hamilton'.
(Answers Dev) Restored edit

Sign in to comment.

Answers (2)

countries = {'Finland','San Marino','Serbia','France','Germany','Spain','Ireland','Italy','United Kingdom','Bosnia and Herzegovina'}; % not an exaustive list
countries = cellfun(@(c)[c ' '],countries,'uni',0); % add a space to the end of the country name like this or just add it above
celldata = {'United Kingdom Lewis Ham'; 'Germany Mike Schumi'; 'Germany Seb Vettel'; 'Finland Kimi Raikkoenen'};
celloutput = erase(celldata, countries);
celloutput = replace(celloutput,' ','_');
P.S. The code you provided is not very informative, sharing the small cell data file would have been more useful. These advanced string operations functions were introduced in R2016b, otherwise you can do the same with a loop or cellfun.
You might need a list of country names like above, or all the possible strings that could appear at the beginninng of the cell. If you want to get it from somewhere the Mapping Toolbox could help if you have it, otherwise scraping it from a website, but I guess it'd be faster to just copy-paste and create one. Another simpler method if there are no names with 3 parts or more ('Nyck de Vries, 'Juan Manuel Fangio'), you could just take the last 2 words if you are sure that they are all going to be 2-part like those in the example
Can we make the assumption that the name always is the final two words in the cell, and the country is everything before that? Like, if ca is your cell array:
for k = 1 : numel(ca)
% Extract char array from cell.
thisCellContents = strtrim(ca{k});
fprintf('Processing #%d of %d : "%s".\n', k, numel(ca), thisCellContents); % Print current item
% Find space indexes.
spaceLocations = find(thisCellContents == ' ');
% Extract driver name
driverName{k} = thisCellContents(spaceLocations(end-1) + 1 : end)
% Extract country name.
countryName{k} = thisCellContents(1 : spaceLocations(end-1) - 1)
end
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

Asked:

iii
on 2 Jan 2023

Commented:

on 24 Jan 2023

Community Treasure Hunt

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

Start Hunting!