Trying to add data pulled from trials subfolders into a table and add a column in that table that labels the data with the trial that is came from

I have code right now that after the user selects a file there is are mutiple subfolders labeled with trial numbers. Within the each trial folder is anotehr subfolder "Data" that matlab pulls in as a text file "imudata.txt" and bring it into the workspace as a table.
the file structure is like this
  • pt_01 folder
  • - Trial 1
  • -> Data
  • imudata.txt
  • - Trial 2
  • -> Data
  • imudata.tx
  • - Trial 3
  • -> Data
  • imudata.txt
the code is:
P = uigetdir('C:\user\project1\');
F = fullfile(P,'Trial*','Data','imudata.txt');
S = dir(F);
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
T = readtable(F); % pick a suitable file importing function and options.
S(k).data = T;
end
I want the data to add all of the data into a table with other data from each trial and have create a colum in the table that labels which trial the data came from. So it looks something like this:
Here is what im thingking so far but im not sure if it is right.
Creating a table with Data from Each Trial
for i= 1:numel(T)
Table = [S(i).data,CDData(i).data]
Trial = i
Table_i= addvars(Table,Trial);
end
Any help is greatly appreciated

 Akzeptierte Antwort

P = uigetdir('C:\user\project1\');
F = fullfile(P,'Trial*','Data','imudata.txt');
S = dir(F);
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
T = readtable(F);
trial_name = regexp(S(k).folder,'\\(Trial.*)\\','tokens','once');
new_data = repmat(trial_name,size(T,1),1);
T = addvars(T,new_data,'NewVariableNames',{'Trial'},'Before',1);
S(k).data = T;
end

5 Kommentare

Hi there thnak you for your help! I just tried running this code and go this error in the T=addvars line:
'Error using . "
To assign to or create a variable in a table, the number of rows must match the height of the table.
b = move(b).dotAssign(newvarnames{ii},varargin{ii}); % b.(newvarnames{ii}) = varargin{ii}
Is this an issue with the new_data variable, do you have and suggestions on how to fix this?
Thank you so much
It also says that trial_name and new_data are a 0x0 cell so maybe their is an issue with those variables
I just fixed that issue never mind! It left me with a 1x10 structue (with data from all 10 trials) stored in S in the workspace shown here. It there any way to vertically concatenate all this data into one table outside the loop?
"[Is] there any way to vertically concatenate all this data into one table outside the loop?"
% after the loop:
T = vertcat(S.data)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Gefragt:

am 21 Feb. 2024

Kommentiert:

am 21 Feb. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by