How to have Row Names extend to empty rows.
1 view (last 30 days)
Show older comments
Hello,
I am importing a data in txt file. Have a couple Rows where the Channel name or row name is in the first column under Var 1.When i import there are sections within the data (Data_in) where the Under Var1 you would find White Spaces but the corresponding Columns would have data. As the original txt file where this is being importated from it was a little matrix with in a table.
EXAMPLE: Here all the rows and Column data belongs to Var 1 ABC but I try to extract only First row of data agnist ABC shows up and not the ROWS below.
VAR1 VAR2 Var3 Var 4
ABC 5 15 23
10 20 25
55 85 65
Below is Screenshot of my results.
Also Attaching my Script and File to the question.
3 Comments
Voss
on 5 Mar 2024
"the data below {'C_CluActForceMapDownY'} until {'C_CluActForceMapUpY' } is associated with {'C_CluActForceMapDownY'}"
I understand.
My question was more about Var3 vs Var4 and Var7 vs Var8. Like in the case of 'C_ADS_ModeSupportKeyOnTimeout' non-NaN data shows up in Var4 and Var8, but for 'C_CluActForceMapDownY' it's in Var3 and Var7. This is due to how readtable interprets the file, and it seems likely to me that this is not a desirable way to store the data. In particular, there's never more than 2 non-NaN values in Var3 through Var8 in any row.
filename = 'C3_NGHD_E369.e001.p000_Freightliner_DD13_2021_ModeAB_11Spd_C3_1.txt';
Data_in = readtable(filename, 'Delimiter',{',',';',' '});
% number of non-NaN values in each row, for Var3 through Var8:
n = sum(~isnan(Data_in{:,3:end}),2);
nnz(n == 0) % 1993 rows have all NaNs in Var3 through Var8
nnz(n == 1) % 43 rows have one non-NaN value in Var3 through Var8
nnz(n == 2) % 50557 rows have two non-NaN values in Var3 through Var8
nnz(n > 2) % no row has more than 2 non-NaN values in Var3 through Var8
So Var3 through Var8 could conceivably be collapsed into two columns of mostly non-NaN data, by making appropriate changes to the readtable call and/or by appropriate post-processing of the table returned from readtable.
Would it be better for your purposes to have just two columns of numeric data? Or do you need all those NaNs in there (which are merely an artifact of what readtable does with this crazy file format)?
In any case, it's something to think about, I think. Regardless, I've gone ahead and answered the question you asked.
Accepted Answer
Voss
on 5 Mar 2024
filename = 'C3_NGHD_E369.e001.p000_Freightliner_DD13_2021_ModeAB_11Spd_C3_1.txt';
T = readtable(filename, 'Delimiter',{',',';',' '});
disp(T(114:130,:)) % example region with ':' Var1
is_empty = strcmp(T.Var1,':').';
s_idx = strfind([false is_empty],[false true]);
e_idx = strfind([is_empty false],[true false]);
for ii = 1:numel(s_idx)
T{s_idx(ii):e_idx(ii),1} = T{s_idx(ii)-1,1};
end
disp(T(114:130,:)) % now filled in
6 Comments
More Answers (0)
See Also
Categories
Find more on Data Import and Analysis 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!