Splitting up data in a single cell to a multiple columns array

Hi,
I have a table of data that is 3275x1. In each cell, there is a large amount of data in groups of threes, each seperated by semi-colons. The pattern of the data in each cell is 'X-coordinate, Y-coordinate, T-value; X-coordinate(2), Y-coordinate(2), T-value(2);' (and so on, many times). For instance, part of the data of a single cell looks like the following (Importantly, each cell starts and ends with an apostraphe ('):
'530,510,165713557972;530,511,16523281757976;631,512,1657123213423478;...etc.
So here, 530 is the first X-coordinate, 510 is the first Y-coordinate, and 1657[...] is the first T-value. Then after the semi-colon, comes the second X, Y and T values. This pattern is repeated many more times in each cell.
I'm currently trying to split up the data into three different arrays - one with X-values, one with Y-values, and one with T-values. I imagine you'd need some sort of for loop to split the data up like this, but I'm afraid I'm struggling to figure out how to do this.
I hope the question makes sense, and any help would be greatly appreciated!
Thanks a lot.

4 Comments

"Importantly, each cell starts and ends with an apostraphe (')"
Did you actually check if the data really start & end with apostrophes (if so, how?), or are you just confusing how data are displayed with what data are stored in memory?
"I imagine you'd need some sort of for loop to split the data up like this"
I don't see why:
str = '530,510,165713557972;530,511,16523281757976;631,512,1657123213423478;...';
tkn = regexp(str,'(\d+),(\d+),(\d+);','tokens');
tkn = vertcat(tkn{:})
tkn = 3×3 cell array
{'530'} {'510'} {'165713557972' } {'530'} {'511'} {'16523281757976' } {'631'} {'512'} {'1657123213423478'}
format long G
mat = str2double(tkn)
mat = 3×3
1.0e+00 * 530 510 165713557972 530 511 16523281757976 631 512 1.65712321342348e+15
Thanks so much for your answer.
Apologies, I was just confusing how the data is displayed, so ignore the apostraphe.
The reason I thought there'd be a loop is that I wanted to create some code that went through my 3275x1 array and do this for all rows, but perhaps this isn't necessary (I'm fairly new to MatLab, forgive me!). So, for instance, in one array each row would have extracted only the X-values from the initial row of data, and of course there'd be another for the Y-values, and another for the T-values.
So ideally, I'd find a piece of code of that seperates the X, Y and T values from each row into new columns, so I'd end with either a 3275x3 array with individual columns for the aforementioned variables, or three seperate 3275x1 vectors, one for each variable. I've tried a bunch of different codes, but often have gotten the following error:
Error using regexp
All cells must be char row vectors.
The furthest I've gotten is creating a complicated 3275x1 array, where each cell in the array is roughly 300x1 (it varies between rows). The 300x1 cells were created by splitting the data by semi colons in each row. Within this 300x1 cell, each row contains a 1x3 cell where the individual x, y and t coordinates have been split into individual columns.
To clarify: the data array is called 'data_split' and is a 3275x1 array. data_split{1,1} consists of roughly 300 rows under a single column of 1x3 cells. Inside these 1x3 cells is one x-value, one y-value and one t-value. I want to put together all the x-values inside a unique vector/column, and the same for the y-values and t-values.
I've tried to use your suggested code on one of the rows of the 3275x1 array, but have ended up with a 1x300 cell where each column has one 1x3 cell, where the X,Y and T values are split. As hopefully I've explained above, this isn't quite what I'm looking for.
I hope my explanation is adequate - let me know if there's anything I can clarify.
Again, many thanks for the help.
@Ori Ossmy: what would help is if you uploaded your original data file by clicking the paperclip button.
Most likely this mess can be fixed by some improvements to the file importing.
I've found a good way to sort the data as per Aritra's answer, but thanks so much for the helpful response.

Sign in to comment.

 Accepted Answer

Hi,
As per my understanding you are trying to split up the data of a single cell to a multiple columns array. Since the data is in string format it needs certain preprocessing.
To do so you can make use of the split function followed by the reshape function. The split(str,delimiter) function helps to split a string at delimiters. The reshape(A,sz) function helps to reshape an array to a desired size.
In the below example suppose the example variable needs to be divided into 2x3 matrix. To do so you can use the following code snippet:
example = '530,510,165713557972;530,511,16523281757976';
t = split(example ,{',',';'});
t = reshape(t,[3,2])';
Refer the following pages for more information on split and reshape:

More Answers (0)

Asked:

on 30 Jan 2023

Commented:

on 30 Jan 2023

Community Treasure Hunt

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

Start Hunting!