Extracting Data from Table

I am attempting to sort a large set of 3D data, into 2D frames. For context, the data I have imported is a text file and I want to seperate the text file into seperate tables, which all have the same X value. I had previously done this by creating several ID variables...
idx_250 = FFF1.x_coordinate == 0.25;
x_250 = FF1(idx_250,:);
This was repeated for each X value from 0.25 to 4, in 0.25 intervals. In attempt to neaten this up I tried to create a for loop.
for i = 1 :length(xv1)
id = FFF1.x_coordinate == xv1(i);
idTable = FFF1(id,:);
%Here I will process the data within idTable and save it in an appropriate array
end
Here, xv1 is a vector containing all of my x values [0.25 0.5 ... 4].
However, when i = 1 and xv1(i) therefore equals 0.25, the id vector only has one true value. However if I put: id = FFF1.x_coordinate == 0.25, there is no problem. Could someone please explain why it would work if i directly type 0.25 vs a variable with the same value? Thanks!

1 Comment

@Rhiannon: please click the paperclip button to upload your data in a MAT file.

Sign in to comment.

 Accepted Answer

It would help to have your data.
Lacking it, consider doing something like this —
T1 = table(reshape(repmat((1:5),5,1),[],1), sort(rand(25,1)), randn(25,1), VariableNames=["Class","X","Y"])
T1 = 25x3 table
Class X Y _____ ________ ________ 1 0.043269 0.028793 1 0.053658 0.12042 1 0.12265 -0.58972 1 0.15962 -0.64789 1 0.16202 1.2526 2 0.19557 0.81594 2 0.24494 1.0379 2 0.25965 0.38801 2 0.28422 0.9758 2 0.3173 0.95883 3 0.3423 1.9717 3 0.40383 0.85093 3 0.40679 0.047132 3 0.48328 -2.1233 3 0.5016 0.95197 4 0.55621 -1.2707
[UClass,~,idx] = unique(T1.Class,'stable');
XYc = accumarray(idx, (1:numel(idx)).', [], @(x){T1(x,:)})
XYc = 5x1 cell array
{5x3 table} {5x3 table} {5x3 table} {5x3 table} {5x3 table}
XYc{3}
ans = 5x3 table
Class X Y _____ _______ ________ 3 0.3423 1.9717 3 0.40383 0.85093 3 0.40679 0.047132 3 0.48328 -2.1233 3 0.5016 0.95197
In this instance, all the ‘XYc’ tables are the same sizes, however they don’t have to be, and ‘Class’ do not have to be integers, providing there is a limited number of different values for it. (The'stable' argument simply returns them in the order the unique function encountered them, rather than sorting them as well.)
Example —
T1 = table(randi(5,25,1)*rand, sort(rand(25,1)), randn(25,1), VariableNames=["Class","X","Y"])
T1 = 25x3 table
Class X Y _______ ________ _________ 0.15684 0.010617 -1.0611 0.62738 0.022529 0.84091 0.47053 0.05813 -0.89505 0.31369 0.07921 -0.69426 0.31369 0.085088 0.0017908 0.47053 0.11078 0.067547 0.62738 0.11444 1.26 0.15684 0.2486 1.0515 0.47053 0.33631 -0.2269 0.15684 0.34201 -1.9554 0.62738 0.35836 -1.2822 0.47053 0.39938 -0.16623 0.31369 0.4293 1.398 0.31369 0.43839 0.95893 0.47053 0.46436 -0.2051 0.47053 0.48195 0.3377
[UClass,~,idx] = unique(T1.Class,'stable');
XYc = accumarray(idx, (1:numel(idx)).', [], @(x){T1(x,:)})
XYc = 5x1 cell array
{3x3 table} {6x3 table} {9x3 table} {5x3 table} {2x3 table}
XYc{1}
ans = 3x3 table
Class X Y _______ ________ _______ 0.15684 0.010617 -1.0611 0.15684 0.2486 1.0515 0.15684 0.34201 -1.9554
XYc{3}
ans = 9x3 table
Class X Y _______ _______ ________ 0.47053 0.05813 -0.89505 0.47053 0.11078 0.067547 0.47053 0.33631 -0.2269 0.47053 0.39938 -0.16623 0.47053 0.46436 -0.2051 0.47053 0.48195 0.3377 0.47053 0.58899 1.198 0.47053 0.8856 1.1108 0.47053 0.95445 0.60166
XYc{5}
ans = 2x3 table
Class X Y _______ _______ ________ 0.78422 0.52813 -0.2259 0.78422 0.90808 -0.30591
.
EDIT — Corrected typographical errors.
.

2 Comments

Hi,
Thank you for your answer, I appreciate you taking the time. I have implemented successfully implemented your procedure!
All the best
As always, my pleasure!

Sign in to comment.

More Answers (1)

If FFF1.x_coordinate == xv1(i) gives a different result than FFF1.x_coordinate == 0.25, then xv1(i) is not equal to 0.25. You don't show how xv1 is constructed, so I can't comment on why xv1(1) is not exactly 0.25.
Regardless, here is a way to split a table into a cell array of tables based on the value of one table variable (in this case 'x'):
x = randi(16,100,1)/4; % x: random values amongst {0.25, 0.5, ..., 3.75, 4}
y = randn(100,1); % y: some other random values
z = randn(100,1); % z: some other random values
T = table(x,y,z)
T = 100x3 table
x y z ____ _________ _________ 2.25 0.11897 -1.5427 1 0.40671 1.9308 2.25 -0.56589 0.93971 3.25 0.17427 1.2856 3.5 -0.093726 1.0214 4 0.031567 0.25919 1.75 1.2622 1.5087 1.25 1.1321 0.72386 1 -1.6124 -0.5274 0.5 -0.16188 -0.66869 0.25 -0.2583 -0.079574 0.5 -0.98999 -1.9855 1.5 -1.0284 -0.31855 0.5 -0.072366 0.23192 1.25 -0.23006 -0.90122 3 -1.375 0.82931
% split T into cell array of tables C, based on value of T.x
vars = T.Properties.VariableNames;
C = splitapply(@(varargin){table(varargin{:},'VariableNames',vars)},T,findgroups(T.x))
C = 15x1 cell array
{ 8x3 table} { 5x3 table} { 5x3 table} { 7x3 table} { 5x3 table} { 7x3 table} { 7x3 table} { 5x3 table} { 6x3 table} { 6x3 table} { 5x3 table} {11x3 table} { 8x3 table} { 6x3 table} { 9x3 table}
% show the tables contained in cell array C
C{:}
ans = 8x3 table
x y z ____ ________ __________ 0.25 -0.2583 -0.079574 0.25 1.11 -0.65081 0.25 -0.9458 -1.1575 0.25 1.5975 -0.030275 0.25 -1.55 0.39269 0.25 -0.73644 -0.0045602 0.25 -0.32405 0.1475 0.25 -0.77887 0.23531
ans = 5x3 table
x y z ___ _________ ________ 0.5 -0.16188 -0.66869 0.5 -0.98999 -1.9855 0.5 -0.072366 0.23192 0.5 1.1782 0.18323 0.5 -1.7053 -0.98583
ans = 5x3 table
x y z _ ________ ________ 1 0.40671 1.9308 1 -1.6124 -0.5274 1 1.9243 -0.02964 1 -1.7024 0.30671 1 -0.72628 -0.45578
ans = 7x3 table
x y z ____ _________ ________ 1.25 1.1321 0.72386 1.25 -0.23006 -0.90122 1.25 0.14016 -0.41135 1.25 -0.083017 -1.6199 1.25 0.49901 -0.22053 1.25 -1.3333 -0.24507 1.25 -0.88313 0.68772
ans = 5x3 table
x y z ___ ________ ________ 1.5 -1.0284 -0.31855 1.5 0.78943 -1.8405 1.5 1.1048 1.665 1.5 0.44321 -0.35164 1.5 -0.53269 -0.13423
ans = 7x3 table
x y z ____ _______ _______ 1.75 1.2622 1.5087 1.75 -1.4937 1.1163 1.75 0.90235 2.1495 1.75 2.2662 1.2343 1.75 0.65392 0.63818 1.75 0.25772 1.1014 1.75 -1.4749 1.4902
ans = 7x3 table
x y z _ ________ ________ 2 -1.0289 -0.478 2 0.1122 0.434 2 1.9323 -0.95664 2 -0.88587 0.64639 2 1.5465 -0.1029 2 -0.30542 0.12073 2 1.4805 0.015096
ans = 5x3 table
x y z ____ ________ ________ 2.25 0.11897 -1.5427 2.25 -0.56589 0.93971 2.25 1.7936 -0.90235 2.25 1.0833 -0.93631 2.25 0.90805 1.9975
ans = 6x3 table
x y z ___ ________ ________ 2.5 0.20708 -0.92896 2.5 1.6666 -1.289 2.5 -1.0514 -0.25642 2.5 -0.71928 -0.5503 2.5 -1.7355 -0.41085 2.5 0.46468 0.19258
ans = 6x3 table
x y z ____ ________ ________ 2.75 1.2641 0.32202 2.75 2.5691 0.34345 2.75 1.2653 -0.15336 2.75 -0.87514 1.4729 2.75 1.943 -1.2511 2.75 0.4632 0.54318
ans = 5x3 table
x y z _ ________ ________ 3 -1.375 0.82931 3 -0.46188 1.5111 3 -1.3897 -1.7481 3 0.43772 -0.13668 3 -0.32444 -1.2576
ans = 11x3 table
x y z ____ ________ _________ 3.25 0.17427 1.2856 3.25 0.53059 -0.57649 3.25 0.22523 0.45576 3.25 -0.24738 0.53447 3.25 -0.6151 0.23264 3.25 1.517 -0.34507 3.25 1.047 1.6549 3.25 1.6717 0.90479 3.25 0.48414 -0.90945 3.25 1.3229 -0.019966 3.25 -1.1691 -0.91074
ans = 8x3 table
x y z ___ _________ _________ 3.5 -0.093726 1.0214 3.5 0.51092 2.2424 3.5 -1.2459 0.47417 3.5 0.72009 -0.31976 3.5 -1.4277 -0.35091 3.5 -0.34765 1.6445 3.5 -0.33764 0.80268 3.5 1.6172 -0.020338
ans = 6x3 table
x y z ____ _______ ________ 3.75 -0.1864 -1.2987 3.75 0.21496 -1.1633 3.75 -1.4814 0.78726 3.75 1.9162 -0.11848 3.75 0.79221 -1.4294 3.75 1.2749 0.023442
ans = 9x3 table
x y z _ _________ _________ 4 0.031567 0.25919 4 -0.44974 0.72412 4 0.19819 -0.48513 4 -0.010109 -0.006922 4 -0.54603 1.055 4 -1.7345 1.5203 4 -0.045592 0.87809 4 0.50318 0.67836 4 -0.12952 -0.27911

1 Comment

Thank you for taking the time to answer my question, i really appreciate it

Sign in to comment.

Categories

Tags

Community Treasure Hunt

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

Start Hunting!