How can I split the data in cells

7 visualizzazioni (ultimi 30 giorni)
iii
iii il 2 Gen 2023
Commentato: Rena Berman il 24 Gen 2023
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 Commenti
Stephen23
Stephen23 il 3 Gen 2023
Modificato: Stephen23 il 3 Gen 2023
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'.
Rena Berman
Rena Berman il 24 Gen 2023
(Answers Dev) Restored edit

Accedi per commentare.

Risposte (2)

MarKf
MarKf il 2 Gen 2023
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

Image Analyst
Image Analyst il 2 Gen 2023
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:

Categorie

Scopri di più su Data Type Identification in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by