How to split an array cell with multiple text line with carriage return

10 views (last 30 days)
I have ingested an excel file that has one cell with multiple strings with each separated by carriage return...ie...
abc(carriage return symbol)efg(carriage return symbol)hij(cr symbol)...ect in on cell array. As a result, when I say array(i) has a value of abc(carriage return symbol)efg(carriage return symbol)hij(cr symbol). I want to have array(1) have values of abc array(2) of efg.......ect. Separate each text by the Carriage return symbol at the end and put it in new cell.

Answers (1)

dpb
dpb on 19 Oct 2022
We can't test without actual data sample to be sure what is actually embedded in the string, but if it's using standard whitespace, then simply
array=split(array);
should do the trick.
If it doesn't, attach the actual array as a .mat file or the Excel file itself.
Depending upon the content of that file and how you read it, you may be able to avoid the need by using proper syntax/options with which to read the file to begin with.
  4 Comments
Nischal Amin
Nischal Amin on 20 Oct 2022
I have attached two files. Run Matlab script and pick the excel file. I would like X to be...
x = [abc, def, ghi, jkl, mno, pqr, stu, vwx, yz, ABC]. Size of x = 1x10.
It's showing...
x = {'mno'} {'pqr'} {'stu'}
{'vwx'} {'yz' } {'abc'}
X size of 2 x 3
dpb
dpb on 20 Oct 2022
Edited: dpb on 20 Oct 2022
The problem there is in the format of the input file -- you've got stuff scattered around all over creation in multiple cells and expecting it to be smooshed all back together again.
Why would you do that? If all the strings are supposed to be in one column, then putting them there in the input file into a single column would be the logical thing to do.
It can be forced to do so, but it's ugly...
My release (R2020b) won't read string data with readmatrix so I used readcell instead, but one way to beat it into the desired form is..
data=readcell('try.xlsx','NumHeaderLines',1);
data=data(cellfun(@(c)all(~ismissing(c)),data,'UniformOutput',1));
data=split(join(data,newline));
But, I'd still strongly recommend to not stuff your data into such an unstructured format. Obviously, this is just an example of some other "real" file structure, and maybe it's not possible to avoid such a mess, but do so if possibly can.
split unfortunately, doesn't have a way to handle the different number of delimiters in the various cells and so one gets into an infinite loop of cell arrays by any application of it inside cellfun or arrayfun because the 'uniformoutput',0 flag has to be set and so one just gets back the same cell array form started with.
So, the "trick" is to string all the cells together, also joining them with a newline and split() the resulting single long string...
It is that single string which is the format you showed in the original post that led to the provided answer -- but, that showed that that wasn't actually the data that had in that form -- yet.

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!