Effacer les filtres
Effacer les filtres

Identifying blocks of data from a cell array having the same header

1 vue (au cours des 30 derniers jours)
Saeid
Saeid le 28 Mai 2019
Commenté : Saeid le 29 Mai 2019
I usually use xlsread to load large excel datasheets for further manipulation. The excel worksheet looks somewhat like this:
Sometimes I need to isolate all the columns that have the same header, i.e., in the above case I need to separate all columns under the Orange, Red and Green headers, and create new arrays from those. Is it possible to have an array just composed of the headers, and maybe another array giving the starting and the ending number of the columns? The Output should be something like:
HeaderArray= {'Day 12' 'Night 24' 'Morning 3'}
HeaderStart=[1 5 11]
HeaderEnd=[4 10 13]

Réponses (1)

Guillaume
Guillaume le 29 Mai 2019
Assuming that the runs of identical headers are always continuous
header = repelem({'Day 12', 'Night 24', 'Morning 3'}, [4, 6, 3]); %construct demo data
[HeaderArray, ~, id] = unique(header, 'stable');
locs = find(diff([0; id; 0]) ~= 0);
HeaderStart = locs(1:end-1);
HeaderEnd = locs(2:end) - 1;

Catégories

En savoir plus sur Data Import from MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by