Unique name detection in table headers
1 view (last 30 days)
Show older comments
Hi,
I have a bunch of data that I need to process. My data consists of plot information with multiple traces within one plot. The generated data is written into multiple tables within one .txt file. Each table represents the information of a single trace. As each trace within a file belongs to the same plot, all tables have the same amount of data points, resulting in an equal number of rows and sorting (This makes my life a lot easier) A picture is attached to this post. However, the actual information of a single trace is saved in the last column, the other colums show parameters that the data point is corresponding to. This results in a lot of abundant stuff within a single file that does not provide any additional information.
As all tables have the same formatting and sorting, I want to extract the last column of each table within the file and generate one single table from it. I was thinking of a solution in which a new column is created whenever a new header name is detected, which would allow for a variable amount of traces to be detected. I do not have the programming knowledge to pull this off however, any tip how to tackle this problem is greatly appreciated.
2 Comments
Stephen23
on 4 May 2023
Please upload a representative data file by clicking the paperclip button.
This may be your actual data, or if that cannot be distributed, random data with exactly the same file format.
Answers (3)
Vilém Frynta
on 4 May 2023
Edited: Vilém Frynta
on 4 May 2023
Hello,
I extracted all your data from last columns, which is 300 numbers. And because I knew that every table contains 10 values, I could easily reshape this 300x1 long vector into 10x30 table, where each column = last column of every table.
There's more approaches, and the one you brought up (new column is created whenever a new header is detected) could definitely work. But when we know that every 10 values are 1 column, we can just use this knowledge + indexing + reshaping and do it in more simple way.
Hope it's understandable and that I understand your question correctly.
Hope I helped.
T = readtable('test_data_for_forum.txt');
idx = ~isnan(table2array(T(:,4))); % index of ALL your numbers
v = table2array(T(idx,4)); % all your numbers from last column
v = reshape(v, 10, 30) % reshape 300 numbers into table
% each column = last column from each table
0 Comments
Siddharth Bhutiya
on 4 May 2023
If the values in the common variables across all your tables are the same then this can be done with a simple outerjoin operation.
A = [1;2;3;4];
B = [4;5;6;7];
C = [10;20;30;40];
D = [11;22;33;44];
E = [1;2;3;4];
t1 = table(A,B,C)
t2 = table(A,B,C,D)
t3 = table(A,B,E)
t1 = outerjoin(t1,t2,MergeKeys=true)
t1 = outerjoin(t1,t3,MergeKeys=true)
0 Comments
Stephen23
on 5 May 2023
The old-fashioned way:
D = {}; % data
H = {}; % header
fid = fopen('test_data_for_forum.txt','rt');
while ~feof(fid)
H{end+1} = fgetl(fid);
D(end+1) = textscan(fid,'%f%f%f%f','CollectOutput',true,'Delimiter','\t');
end
fclose(fid);
A = cat(3,D{:});
M = squeeze(A(:,end,:))
plot(M)
0 Comments
See Also
Categories
Find more on Data Type Conversion 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!