Automating length with reshaping, and using reshape to loop through data.

4 views (last 30 days)
Hello,
I currently have a 3275x1 vector, named mouse_coord_array. Each cell has three specific data points repeated many times. For instance, I’ll have a series of X-values, Y-values and T(time)-values. The data is split in the following way:
X, Y, T; X2, Y2, T2; X3, Y3, T3; X4, Y4, T4; X5, Y5, T5; X6, Y6, T6;…(and so on). In practice, it looks like this:
3.4,3.1,401232;4.6,4.2,451232; 5.2,3.1,53123; 5.4,3.5,4124214;[…](and so on).
Then, the second row will have a whole new set of numbers. The third will have another new set, and the fourth, and so on.
I’ve managed to use the following code to split the x, y and t values into individual columns for each single row:
example = mouse_coord_array(1,:)
t = split(example ,{',',';'});
t = reshape(t,[3,312])';
% I put 312 as I new the number of rows for this specific cell. However this won’t be the case for the other cells of data.
However, the amount of data in each row varies, so I won’t always want 312 rows.
I have two questions:
  1. How would I automate the length of t as to cater to the amount of data is in each row? This is for when I don't know what the length of my reshaped data will be.
  2. Once I’ve figured this out, I intend on splitting the data for each of the indiviudal 3275 rows. This is another part I’m stuck on - I can’t seem to figure out how to loop this for each row, so that the data is split into X, Y and T for each individual row of mouse_coord_array.
Any help would be greatly appreciated!
I'm fairly new to MatLab so apologies if some of my terminology is incorrect.
Thanks so much for your help!
  2 Comments
Jan
Jan on 31 Jan 2023
"I currently have a 3275x1 vector" - 3275 is not divisable by 3. Therefore it cannot be a list of X,Y,T; ... values.
"Each cell has three specific data points" - "Cells" are elements of cell arrays. You are talking of a numerical array? Then these are "elements".
"t = split(example ,{',',';'});" - The actual input are strings or cell strings?
Billy
Billy on 31 Jan 2023
Thanks for your response, Jan. To clarify:
-- The list of X,Y,T;.. values are within each cell, therefore I don't see how it is relevant whether 3275 is divisable by 3. I'd like to end up with a 3275x3 array, where each column only holds X-values, Y-values, or T-values (one column for each).
-- I've realised that mouse_coord_array is actually described as a cell array in my matlab file - apologies for this confusion. The data was imported in table format and I used the 'table2array' function to change it to an array, and the result is a cell array. Here's the code I used:
mouse_coord = readtable(['data.csv']);
mouse_coord_array = table2array(mouse_coord);
Thanks for the help!

Sign in to comment.

Accepted Answer

Jan
Jan on 31 Jan 2023
Edited: Jan on 31 Jan 2023
See reshape - simply use [] for the free dimension to be adjusted automatically:
tt = reshape(t, 3, []).'
Note: While ' is the abbreviation for ctranspose, .' is transpose. This does not change the result for real input values, but it is clearer.
Afterwards the X values are:
X = tt(:, 1)
Or with the original data:
X = t(1:3:end)
  3 Comments
Billy
Billy on 31 Jan 2023
Also to clarify, I want to end up with a cell array which is 3275x3 with each column pertaining to only X-values, Y-values or T-values (individually). This piece of code has enabled me to seperate the X, Y and T values of one single cell of data which is great, but I'm looking to do this for all 3275 rows.
Jan
Jan on 1 Feb 2023
I'm still not sure, if I understand, what the inputs are. After:
t = split(example ,{',',';'});
t seems to be a string array. Do you want a numerical output or really cell arrays?

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!