How to create separate tables from a data set
이전 댓글 표시
Working with gait analysis data, the exported raw file separates the data into four "tables" on top of each other (Joints, Model outputs, Segments and Trajectories).
When I use the readtable function, it identifies the variables from the top table (Joints) but not the other tables and instead has 5 rows of NaN separating the tables.
How do I get my code to identify and separate the data into easier to read tables.
(Attached is the file as a .txt, the file was too large to attach as .csv which is what I've been working with)
댓글 수: 3
dpb
2021년 2월 10일
How about cutting the file size down to just a few lines of each section and (say) a half-dozen variables so folks can see what the organization is that you know about?
The size won't make any difference to the code but would make understanding the problem much simpler for folks here...
Jak
2021년 2월 10일
Image Analyst
2021년 2월 10일
Please explain what column numbers of that are supposed to be what table. And do you want an array of tables (one table for each patient) or do you want all patients in the same table with the patient ID as one of the columns (so there is just a single set of 4 tables)?
채택된 답변
추가 답변 (1개)
Mathieu NOE
2021년 2월 10일
hello
here below my suggestion to split the large csv file into smaller txt files
they may be smarter use of regexp but this is what I can offer quick and dirty
seems you can now easily do a second loop to load these files with readtable and generate your multiples tables from there;
hope it helps, even if it can certainly be further refined
% Tell it what folder you want to put the files in
outdir = cd;
% Read the initial file in all at once
filename = 'Level Reduced.csv';
fid = fopen(filename, 'r');
data = fread(fid, '*char').';
fclose(fid);
% Break it into pieces based upon ',,,,,,,,,,,,,,,,,,,,,,,,' lines
pieces = regexp(data, '\n\s*\n', 'split');
[match,piece,startIndex,endIndex] = regexp(data,',,,,,,,,,,,,,,,,,,,,,,,,','match','split');
% Now loop through and save each one
n = 0;
for k = 1:numel(piece)
if size(piece{k},2) > 100 % so avoid storing empty files or single line (title)
n = n + 1;
filename = fullfile(outdir, ['out' num2str(n), '.txt']);
% Now write the piece to the file
fid = fopen(filename, 'w');
fwrite(fid, piece{k});
fclose(fid);
end
end
댓글 수: 1
Mathieu NOE
2021년 2월 10일
just noticed this line has to be removed :
pieces = regexp(data, '\n\s*\n', 'split');
카테고리
도움말 센터 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!