Separate multi-word comma separated cell into rows
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Maddie Long
am 15 Mär. 2022
Bearbeitet: Peter Perkins
am 17 Mär. 2022
I would like to seperate the name of a business and the number attached to the name into 2 columns and then at the comma start a new row in the table:
My input:
{'Business Name One[5],Business Name Two[3],Business Name Three[2]'}
{'Business Name One[5]'}
{'Business Name Two[3],Business Name Three[2]'}
and I need it to output:
'Business Name One' | 5
'Business Name Two' | 3
...
I have tried
unqValue=unique(a.data);
cell_dat_split = cellfun(@(x) sscanf(x, '%s %s %s, '), unqValue, 'Uniform', false);
But its just not doing quite what I want, any help would be great!
0 Kommentare
Akzeptierte Antwort
Stephen23
am 15 Mär. 2022
More efficient than using CELLFUN:
C = {'Business Name One[5],Business Name Two[3],Business Name Three[2]'; ...
'Business Name One[5]'; ...
'Business Name Two[3],Business Name Three[2]'};
D = regexp(sprintf('%s,',C{:}),'([^,]+)\[(\d+)\]','tokens');
D = vertcat(D{:})
Weitere Antworten (2)
Peter Perkins
am 17 Mär. 2022
Bearbeitet: Peter Perkins
am 17 Mär. 2022
[Edited to be even modern by using split, not strsplit, join, not strjoin, and double, not str2double.]
Here's a more modern version, using string and patterns, which are really the best way to go these days. There's a bit of trickiness at the beginning, going from ragged cellstr to string, but after that it's smooth sailing.
C = {'Business Name One[5],Business Name Two[3],Business Name Three[2]';
'Business Name One[5]';
'Business Name Two[3],Business Name Three[2]'}
Turn that "ragged" cell array into one long string, then split at the commas.
S = join(string(C),',')
S = split(S,","); S = S(:)
Now it's just a matter of pulling out the text and the numbers. Pattern is much easier to use than regexp.
pat = "[" + digitsPattern(1) + "]";
biz = extractBefore(S,pat)
buz = extract(S,digitsPattern)
Now convert them to more useful types and put them in a table.
biz = categorical(biz);
buz = double(buz);
t = table(biz,buz)
0 Kommentare
Voss
am 15 Mär. 2022
C = { ...
'Business Name One[5],Business Name Two[3],Business Name Three[2]'; ...
'Business Name One[5]'; ...
'Business Name Two[3],Business Name Three[2]'; ...
}
C = cellfun(@(x)strsplit(x,','),C,'UniformOutput',false);
C = [C{:}].'
C = regexp(C,'([^\[]+)\[(\d+)\]','tokens');
C = [C{:}]
C = vertcat(C{:})
2 Kommentare
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!