Splitting out a cell that is a combination of numbers and strings

Hi all,
I have the following cell array:
A =
[]
12
[]
[27;28;29;30;31;32]
[]
[]
[73;74]
86
[]
101
[]
)
I would like to know how to split this out at the delimiter ';' so that the strings are in their own cells.Kind of like Excels function 'Text to Columns' where you can split based on a certain delimiter.

6 Comments

Assume you have the function, txt2col. What would B look like?
B = txt2col( A, 'delimiter' );
The cell array in your example does not have any string in it. Only empty matrices, scalars (= 1*1 matrix) and columns vectors (= m*1 matrices). Is it really the structure of your cell array or did you mean:
A =
''
'12'
''
'27;28;29;30;31;32'
...
Yes sorry your correct Guillaume.
txt2col? does that function even exist. I'm on 2013B
No, text2col does not exist. The purpose of per isakson's question was to get an idea of the output you are expecting. Then we can tell you how to write that hypothetical text2col.
Which part am I correct on? That it's a cell array of numbers or that you meant you had a cell array of strings?
Well for example sake, if I said A = {'12', '27;28;29', '[]'} then yes I would say this is a cell array of strings.

Sign in to comment.

Answers (1)

use strsplit or regexp with the split option to split the strings.
s = {'27;28;29'};
c = strsplit(s, ';'); %with strsplit
c = regexp(s, ';', split); %with regexp. may be faster on longer inputs
To do that on every cell of your cell array, use cellfun:
A = {'12', '27;28;29', '[]'};
newA = cellfun(@(s) strsplit(s, ';'), A, 'UniformOutput', false); %or use regexp
newA = [newA{:}] %to redistribute into individual cells.

2 Comments

Robert's answer duplicated here as it should have been a comment:
Actually that didn't quite work and I believe its because the array is infact not completely made of strings. I will explain what I'm actually doing and provide the code also, I'm trying to identify a text in column B against text in column A. B may not be a exact match, as long as it contains some of the partial text (hence I use regexpi inside a for loop to iterate through all of column B inputs). Column A and Column B are both 100% string arrays. This is the code I use for this:
Unique_List = unique(B);
for i = 1:length(B)
index = regexpi(A, Unique_List{i});
index2{i} = find(~cellfun(@isempty,index));
end
So all the matches can output into a cell array index2. However this array has to be a combination of both numbers and strings because using Strsplit function generates an error. Before transposing the output for index2 looks like this:
[] 12 [] [] [] [27;28;29;30;31;32] [] [] [73;74] 13xdouble
I've tried using cellfun function and applying num2str but that creates something weird for those cells with strings (e.g. [73;74]). Not sure how to get around this. Your code is correct for a 100% string array, but I don't know how to convert this into a string. Maybe I'm using the correct code in the loop??
Also to clarify when I mean compare texts, say Column A has Sydney Airport Limited and Column B has Sydney Airport, then index2 should identify this as a match.
Sorry for the long email, but I hope I can get help on this.
The code you've posted is not quite right, so I've no idea what's in index2. In particular,
The end of the loop should be length(Unique_List) not length(B). If the two are equals, there's little point in using unique.
If there are any . * ? $ + ^ \ ( { [ | in B, regexpi is not going to give the result you want. You can use regexptranslate to fix that.
index, returned by regexpi is always going to be a matrix. Hence cellfun on it is never going to work.
Even if it worked, @isempty is always going to return a scalar 0 or 1. Using find on that is just going to also return the same 0 or 1.
In any case, rather than trying to fix something that may or may not be what you want, what are you trying to do in the first place? Given two list A and B, what is the ultimate result you want?

Sign in to comment.

Categories

Asked:

on 25 Nov 2014

Commented:

on 26 Nov 2014

Community Treasure Hunt

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

Start Hunting!