How do I read a block of data from excel sheet and store it in an array?
15 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
harman bhaveja
le 24 Avr 2019
Modifié(e) : per isakson
le 25 Avr 2019
I have ADS-B flight data in an excel sheet. The sheet has groups of flights with corresponding lat, lon. The number of data points for each of the flights are not uniform, each flight has a varying number of data points associarted with it Example flight 1 might have 30 points while flight 2 might have just 10). I want to group all the data for each flight in an array and send it to a function one flight at a time. Could someone help me with the code for this?
I'm attaching the excel file for the same here.
2 commentaires
Réponse acceptée
per isakson
le 24 Avr 2019
Modifié(e) : per isakson
le 25 Avr 2019
This solution uses a containers.Map rather than a cell array because I think there might be gaps in the flight_list. See containers.Map, Object that maps values to unique keys
%%
T = readtable('plot.xlsx', 'ReadVariableNames',true );
flight_list = reshape( unique(T.flight), 1,[] );
flight_data = containers.Map('KeyType','double','ValueType','any');
for jj = flight_list
flight_data(jj) = T{ T.flight==jj, : };
end
read data for flight 12
>> flight_data( 12 )
ans =
12 15 11.133 79.362 35025
12 15 11.162 79.392 35000
12 15 11.184 79.415 34825
12 15 11.207 79.439 34600
12 15 11.215 79.448 34500
12 15 11.252 79.485 34150
...
Added later:
>> flight_data = cssm( 'plot.xlsx' );
where
function flight_data = cssm( ffs )
%%
T = readtable( ffs, 'ReadVariableNames',true );
flight_list = reshape( unique(T.flight), 1,[] );
placeholder = flight_list;
flight_data = containers.Map( flight_list, placeholder, 'UniformValues', false );
for key = flight_list
flight_data(key) = T{ T.flight==key, : };
end
end
4 commentaires
per isakson
le 25 Avr 2019
Modifié(e) : per isakson
le 25 Avr 2019
You sent me a question:
"You said flight_data(12) will give me the table, but if I were to pass a particular column into a function or show output of it, how do I do it?"
No I didn't say "table". In natural English table means something with rows and columns. In Matlab it's a special data type. flight_data(12) actually returns an ordinary matrix of doubles.
Some kind of clever indexing like
>> flight_data(12)(:,3)
Error: ()-indexing must appear last in an index expression.
isn't possible. One has to do it in steps
>> m = flight_data(12);
>> col3 = m(:,3);
>> mean( col3 ) % pass a particular column into a function
ans =
13.33
Plus de réponses (1)
KSSV
le 24 Avr 2019
YOu may proceed like this:
clc ; clear all ;
T = readtable('plot.xlsx') ;
N = length(unique(T.(1))) ;
% loop for each flight
for i = 1:N
idx = T.(1)==i ;
T(idx,:)
end
YOu can access the column you want using T.(1), T.(2) etc. You may also use xlsread as below:
[num,txt,raw] = xlsread('plot.xlsx') ;
N = length(unique(num(:,1))) ;
% loop for each flight
for i = 1:N
idx = num(:,1)==i ;
num(idx,:)
end
4 commentaires
Voir également
Catégories
En savoir plus sur Calendar dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!