How to have Row Names extend to empty rows.

1 Ansicht (letzte 30 Tage)
Sai Gudlur
Sai Gudlur am 5 Mär. 2024
Kommentiert: Voss am 7 Mär. 2024
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 Kommentare
Sai Gudlur
Sai Gudlur am 5 Mär. 2024
Voss,
This is data that comes from various controller and its functions inputs. For instance in the 2nd screenshot that you shared the data below {'C_CluActForceMapDownY'} until {'C_CluActForceMapUpY' } is associated with {'C_CluActForceMapDownY'}. I actually want to perform various operations with multiple columns trying to take Var8 was just an example to see if I can extract the elements associated with one Variable/Parameter.
Voss
Voss am 5 Mär. 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
ans = 1993
nnz(n == 1) % 43 rows have one non-NaN value in Var3 through Var8
ans = 43
nnz(n == 2) % 50557 rows have two non-NaN values in Var3 through Var8
ans = 50557
nnz(n > 2) % no row has more than 2 non-NaN values in Var3 through Var8
ans = 0
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Voss
Voss am 5 Mär. 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
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 _______________________ __________ ____ ____ ____ ____ ____ ____ {':' } {0x0 char} 3725 NaN NaN NaN 3725 NaN {':' } {0x0 char} 3725 NaN NaN NaN 3725 NaN {':' } {0x0 char} 3725 NaN NaN NaN 3725 NaN {':' } {0x0 char} 3725 NaN NaN NaN 3725 NaN {':' } {0x0 char} 3725 NaN NaN NaN 3725 NaN {':' } {0x0 char} 3725 NaN NaN NaN 3725 NaN {':' } {0x0 char} 3725 NaN NaN NaN 3725 NaN {'C_CluActForceMapUpY'} {'[FLOAT'} NaN NaN NaN NaN NaN NaN {':' } {0x0 char} 0 NaN NaN NaN 0 NaN {':' } {0x0 char} 785 NaN NaN NaN 785 NaN {':' } {0x0 char} 1473 NaN NaN NaN 1473 NaN {':' } {0x0 char} 2170 NaN NaN NaN 2170 NaN {':' } {0x0 char} 2851 NaN NaN NaN 2851 NaN {':' } {0x0 char} 3398 NaN NaN NaN 3398 NaN {':' } {0x0 char} 3764 NaN NaN NaN 3764 NaN {':' } {0x0 char} 3915 NaN NaN NaN 3915 NaN {':' } {0x0 char} 4200 NaN NaN NaN 4200 NaN
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
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 _________________________ __________ ____ ____ ____ ____ ____ ____ {'C_CluActForceMapDownY'} {0x0 char} 3725 NaN NaN NaN 3725 NaN {'C_CluActForceMapDownY'} {0x0 char} 3725 NaN NaN NaN 3725 NaN {'C_CluActForceMapDownY'} {0x0 char} 3725 NaN NaN NaN 3725 NaN {'C_CluActForceMapDownY'} {0x0 char} 3725 NaN NaN NaN 3725 NaN {'C_CluActForceMapDownY'} {0x0 char} 3725 NaN NaN NaN 3725 NaN {'C_CluActForceMapDownY'} {0x0 char} 3725 NaN NaN NaN 3725 NaN {'C_CluActForceMapDownY'} {0x0 char} 3725 NaN NaN NaN 3725 NaN {'C_CluActForceMapUpY' } {'[FLOAT'} NaN NaN NaN NaN NaN NaN {'C_CluActForceMapUpY' } {0x0 char} 0 NaN NaN NaN 0 NaN {'C_CluActForceMapUpY' } {0x0 char} 785 NaN NaN NaN 785 NaN {'C_CluActForceMapUpY' } {0x0 char} 1473 NaN NaN NaN 1473 NaN {'C_CluActForceMapUpY' } {0x0 char} 2170 NaN NaN NaN 2170 NaN {'C_CluActForceMapUpY' } {0x0 char} 2851 NaN NaN NaN 2851 NaN {'C_CluActForceMapUpY' } {0x0 char} 3398 NaN NaN NaN 3398 NaN {'C_CluActForceMapUpY' } {0x0 char} 3764 NaN NaN NaN 3764 NaN {'C_CluActForceMapUpY' } {0x0 char} 3915 NaN NaN NaN 3915 NaN {'C_CluActForceMapUpY' } {0x0 char} 4200 NaN NaN NaN 4200 NaN
  6 Kommentare
Sai Gudlur
Sai Gudlur am 7 Mär. 2024
Thank you!!!!! Once Again
You Also suggested I run it prior to the Loop as Directory would be defined ahead of time and doesn't change niether does the extension I was running it in the loop as it was the end was like if it works I could look into Computing time later. But yes it makes complete sense to do it with the headers as we already have the names in filelist.
The line below that you corrected is where is was messing up.
Output_Filename = extractBetween(filelist,"NGHD_",".txt") + ".xlsx"; % <- include the output file extension (I've used .xlsx)
Voss
Voss am 7 Mär. 2024
You're welcome!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Import and Analysis finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by