Table - conditional maths and plots from variables

Dear all,
I have a 57254x87 table, on which I need to perform some maths and plots with the tables. I have one column called Subject_Id (total of 8 particpants) and another one called Trial_Id (a total of 6 per particpant), and I need to plot the data from two others variables. For example, I am looking for a code that will take the first subject, then run a loop for each one of the 6 trials, ploting the data of my columns 3 and 4, and then take the second subject, and reproduces the same thing one more time, and so on for the 8 subjects.
I guess I need one loop inside the other, but I am not sure how to do it.
Any help will be very much appreciated.

3 件のコメント

Chunru
Chunru 2022 年 9 月 19 日
post a portion of your data.
Thiago de Aquino Costa Sousa
Thiago de Aquino Costa Sousa 2022 年 9 月 19 日
@Chunru please considerer the number of the columns, because I was not able to copy the name of th variables. Subject_Id (column 86) Trial_Id (column 87) Xdisplacement (column 3) Ydisplacement (column 4). Thank you for your help.
914371 0.00356000000000000 -0.448190000000000 -3.46237000000000 161.552200000000 -0.0869400000000000 0.360670000000000 88.6375000000000 109.142620000000 514.040590000000 95.0959900000000 0 539.880920000000 83.5962300000000 -103.920560000000 504.995670000000 86.7854700000000 1 1 -3.46237000000000 0.670540000000000 540.376160000000 139.983540000000 0 539.880920000000 83.5962300000000 15.9204300000000 540.506470000000 133.368580000000 21.9610100000000 539.172970000000 108.898650000000 24.9944700000000 529.957210000000 85.0114200000000 10.8748500000000 535.254210000000 2.82422000000000 -17.1000100000000 543.319520000000 132.028410000000 -20.8233700000000 541.126340000000 106.628250000000 -23.7434300000000 528.965700000000 83.6034500000000 -6.90171000000000 534.684630000000 3.54805000000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 160.581540000000 0 0 -1.00000000000000e-05 0 0 160.581540000000 0 0 -1.00000000000000e-05 'Empty' 'None' 'HTY01' 'Trial_1_2Mi'
914372 0.00912000000000000 -0.448190000000000 -3.46237000000000 161.552200000000 -0.0869400000000000 0.360670000000000 88.6375000000000 109.142620000000 514.040590000000 95.0959900000000 0 539.880920000000 83.5962300000000 -103.920560000000 504.995670000000 86.7854700000000 1 1 -3.46237000000000 0.670540000000000 540.376160000000 139.983540000000 0 539.880920000000 83.5962300000000 15.9204300000000 540.506470000000 133.368580000000 21.9610100000000 539.172970000000 108.898650000000 24.9944700000000 529.957210000000 85.0114200000000 10.8748500000000 535.254210000000 2.82422000000000 -17.1000100000000 543.319520000000 132.028410000000 -20.8233700000000 541.126340000000 106.628250000000 -23.7434300000000 528.965700000000 83.6034500000000 -6.90171000000000 534.684630000000 3.54805000000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 160.581540000000 0 0 -1.00000000000000e-05 0 0 160.581540000000 0 0 -1.00000000000000e-05 'Empty' 'None' 'HTY01' 'Trial_1_2Mi'
914373 0.0146700000000000 -0.424390000000000 -3.43538000000000 161.535980000000 -0.0347900000000000 0.281270000000000 88.6616000000000 109.142620000000 514.040590000000 95.0959900000000 0 539.880920000000 83.5962300000000 -103.920560000000 504.995670000000 86.7854700000000 2 1 -3.43538000000000 0.670540000000000 540.376160000000 139.983540000000 0 539.880920000000 83.5962300000000 15.9204300000000 540.506470000000 133.368580000000 21.9610100000000 539.172970000000 108.898650000000 24.9944700000000 529.957210000000 85.0114200000000 10.8748500000000 535.254210000000 2.82422000000000 -17.1000100000000 543.319520000000 132.028410000000 -20.8233700000000 541.126340000000 106.628250000000 -23.7434300000000 528.965700000000 83.6034500000000 -6.90171000000000 534.684630000000 3.54805000000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 160.581540000000 0 0 -1.00000000000000e-05 0 0 160.581540000000 0 0 -1.00000000000000e-05 'Empty' 'None' 'HTY01' 'Trial_1_2Mi'
914374 0.0202300000000000 -0.424390000000000 -3.43538000000000 161.535980000000 -0.0347900000000000 0.281270000000000 88.6616000000000 109.142620000000 514.040590000000 95.0959900000000 0 539.880920000000 83.5962300000000 -103.920560000000 504.995670000000 86.7854700000000 2 1 -3.43538000000000 0.670540000000000 540.376160000000 139.983540000000 0 539.880920000000 83.5962300000000 15.9204300000000 540.506470000000 133.368580000000 21.9610100000000 539.172970000000 108.898650000000 24.9944700000000 529.957210000000 85.0114200000000 10.8748500000000 535.254210000000 2.82422000000000 -17.1000100000000 543.319520000000 132.028410000000 -20.8233700000000 541.126340000000 106.628250000000 -23.7434300000000 528.965700000000 83.6034500000000 -6.90171000000000 534.684630000000 3.54805000000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 160.581540000000 0 0 -1.00000000000000e-05 0 0 160.581540000000 0 0 -1.00000000000000e-05 'Empty' 'None' 'HTY01' 'Trial_1_2Mi'
Chunru
Chunru 2022 年 9 月 19 日
Try to upload a mat file:
data = yourdata(1:200, :); % 1:200 or other number to demonstrate your case

サインインしてコメントする。

 採用された回答

Paul
Paul 2022 年 9 月 19 日

1 投票

Check out splitapply

7 件のコメント

Thiago de Aquino Costa Sousa
Thiago de Aquino Costa Sousa 2022 年 9 月 19 日
Hey Paul, I went through this documentation, and I see that it can solve my problem. However, when I run this code my only output is one plot, even though the documentation says that for each group, it executes the function once. Can you help me on that? I would need to plot 48 times, once for each group, and I also need that for each subject, it holds the plot for add the 6 trials for each one of the 8 participants together. Here it is my code:
[G,subjects] = findgroups(masterTable.Subject_Id,masterTable.Trial_Id);% G has the groupos from a combination of masterTable.Subject_Id,masterTable.Trial_Id, subjects the name of the subjects many times as the number of trials.
func = @(x,y) plot(x,y);
figure
xlim([-150 150]);ylim([-10 510])
subj_trajectory = splitapply(func,masterTable.CameraX,masterTable.CameraY,G);% just return one plot, which one I cannot identify the the group originary.
Adam Danz
Adam Danz 2022 年 9 月 19 日
While splitapply certainly could be used, I don't think it's the best option. A simple for-loop (or two nested for-loops) would likely be faster and more readable.
Thiago de Aquino Costa Sousa
Thiago de Aquino Costa Sousa 2022 年 9 月 19 日
Hi @Adam Danz, thank you very much for you reply. I am pretty new in coding and Matlab. I have this code so far...
clear
clc
mytopfolder = '/Users/.../Participants data';
filepattern = fullfile(mytopfolder, '**/VIVE/Walking_ST/Trial_.txt');
thefiles = dir(filepattern);
nfiles = length(thefiles);
for k = 1:nfiles
basefilename = thefiles(k).name;
fullfilename = fullfile(thefiles(k).folder, basefilename);
fprintf(1, 'Now reading %s\n', fullfilename);
thisTable = readtable(fullfilename);
thisTable.Subject_Id(:) = {fullfilename(98:102)};
thisTable.Trial_Id(:) = {fullfilename(120:130)};
fprintf(' It has %d rows in it.\n', height(thisTable));
if k == 1
masterTable = thisTable;
else
masterTable = [masterTable; thisTable];
end
fprintf(' Now there are a total of %d rows in the master table.\n', height(masterTable));
end
masterTable.CameraX = masterTable.CameraX*-1;
And this is the structure of directory I have
Based on that, would you be able to hep me with this two nested for-loops as I don't have idea how to write it, that allows me to make a plot for each of my Subject associated to the trial Id. Thank you.
Paul
Paul 2022 年 9 月 19 日
Two nested for loops, with a unique() inside each could definitely do the job. I guess I've gotten into the splitapply habit. Don't know which is faster, or which would be considered more readable.
The splitapply approach would go as follows (completely untested code)
splitapply(@(trialid,col3,col4) plottrajid(trialid,col3,col4),T.Trial_ID,T.col3,T.col4,findgroups(T.Subject_Id))
function plottrialid(trialid,col3,col4)
splitapply(@(col3,col4) plotcols(col3,col4),col3,col4,findgrous(trialid))
end
function plotcols(col3,col4)
figure
plot(col3,col4)
xlabel('whatever')
% etc
end
Thiago de Aquino Costa Sousa
Thiago de Aquino Costa Sousa 2022 年 9 月 19 日
Hey Paul. Thank you for replying to me. I corrected what I guess could be some typos, then the code comes to:
splitapply(@(trialid,col3,col4) plottrajid(trialid,col3,col4),T.Trial_ID,T.col3,T.col4,findgroups(T.Subject_Id))
function plottrialid(trialid,col3,col4)
splitapply(@(col3,col4) plotcols(col3,col4),col3,col4,findgrous(trialid))
end
function plotcols(col3,col4)
figure
plot(col3,col4)
xlabel('whatever')
% etc
end
However, when I run the code I got the following error:
Error using splitapply
Applying the function '@(trialid,col3,col4)plottrialid(trialid,col3,col4)' to the 1st group of data generated the
following error: Undefined function 'plottrialid' for input arguments of type 'cell'.
Paul
Paul 2022 年 9 月 20 日
編集済み: Paul 2022 年 9 月 20 日
Only one splitapply is needed
Create some data in a table.
rng(100)
T = table;
T.subj = [ones(10,1); 2*ones(10,1)];
T.trial = 3*randi(2,20,1);
T.col3 = (1:20).';
T.col4 = T.subj + T.trial;
Make the plots
G = findgroups(T.subj,T.trial);
splitapply(@(subj,trial,col3,col4) makeplot(subj(1),trial(1),col3,col4),T.subj,T.trial,T.col3,T.col4,G);
function makeplot(subj,trial,col3,col4)
figure
plot(col3,col4,'-o')
title("subj = " + double(subj) + ", trial = " + double(trial));
end
Thiago de Aquino Costa Sousa
Thiago de Aquino Costa Sousa 2022 年 9 月 21 日
Hey @Paul, thank you for replying to me. I will plot my data on your code and check what happens next... sincerely, Thiago

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

製品

リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by