Error in Matlab Data Filtering from an Excel FiIe

4 views (last 30 days)
I have an Excel file that I want Matlab to read, import, then filter (find code below). My problem is, the RowsToKeep variable outputs as an empty array, even though I know there are rows within the data that match the criteria of MLAT between 72 and 82.
Unfortunately, the Excel file I'm using is too large to upload here, even as a zip file. However, I've attached a snippet of the file here:
Also, after I run the code below, this is the output from Matlab:
Does anyone know why RowsToKeep gets filtered as an empty array, and how I can fix it? My Matlab code is as follows:
clc;
clear all;
close all;
% Read data from Excel file
data = readtable('MLAT_Sat2.xlsx');
% Convert table to array
data = table2array(data);
% Remove rows with any NaN values
data = data(~any(isnan(data), 2), :);
% Extract columns
%NOTE: column extraction begins at row 2 to ignore the column headings.
time = data(2:end, 1);
angle = data(2:end, 2); % degrees
inclination = data(2:end, 3); % degrees
lat = data(2:end, 4); % degrees
lon = data(2:end, 5); % degrees
alt = data(2:end, 6); % kilometers
MLAT = data(2:end, 7); % degrees
MLON = data(2:end, 8); % degrees
r = data(2:end, 9);
% Find rows to keep with MLAT between 72 and 82
RowsToKeep = find(MLAT >= 72 & MLAT <= 82);
% Filter the data based on RowsToKeep
Filter = data(RowsToKeep +1 , :);
filteredTime = Filter(:, 1);
filteredAngle = Filter(:, 2);
filteredInc = Filter(:, 3);
filteredLat = Filter(:, 4);
filteredLon = Filter(:, 5);
filteredAlt = Filter(:, 6);
filteredMlat = Filter(:, 7);
filteredMlon = Filter(:, 8);
filteredr = Filter(:, 9);

Answers (1)

Peter Perkins
Peter Perkins on 31 May 2024
Edited: Peter Perkins on 31 May 2024
This seems like a lot of unnecessary extra code. Let the table you read in do the work for you.
%data = readtable('MLAT_Sat2.xlsx')
% create some fake data
data = table(45658+(0:9)',rand(10,1),rand(10,1),rand(10,1),rand(10,1),VariableNames=["Date" "Time" "Lat" "Lon" "Alt"])
data = 10x5 table
Date Time Lat Lon Alt _____ ________ _________ _______ ________ 45658 0.59816 0.54884 0.38806 0.58854 45659 0.46477 0.10131 0.83326 0.86847 45660 0.32973 0.76736 0.1542 0.12701 45661 0.44286 0.33832 0.57193 0.88036 45662 0.90026 0.0065871 0.15818 0.013161 45663 0.042645 0.029882 0.53624 0.98027 45664 0.11791 0.47429 0.98154 0.96742 45665 0.81422 0.36297 0.99531 0.56691 45666 0.84919 0.8221 0.91991 0.52526 45667 0.37122 0.18142 0.39746 0.37439
% turn those excel timestamps into datetimes and make a timetable
date = datetime(data.Date,ConvertFrom="excel");
time = days(data.Time); % I'm just guessing here
data = table2timetable(data(:,["Lat" "Lon" "Alt"]),RowTimes=date+time)
data = 10x3 timetable
Time Lat Lon Alt ____________________ _________ _______ ________ 01-Jan-2025 14:21:21 0.54884 0.38806 0.58854 02-Jan-2025 11:09:16 0.10131 0.83326 0.86847 03-Jan-2025 07:54:48 0.76736 0.1542 0.12701 04-Jan-2025 10:37:43 0.33832 0.57193 0.88036 05-Jan-2025 21:36:22 0.0065871 0.15818 0.013161 06-Jan-2025 01:01:24 0.029882 0.53624 0.98027 07-Jan-2025 02:49:47 0.47429 0.98154 0.96742 08-Jan-2025 19:32:28 0.36297 0.99531 0.56691 09-Jan-2025 20:22:50 0.8221 0.91991 0.52526 10-Jan-2025 08:54:33 0.18142 0.39746 0.37439
filteredData = data(0.1 <= data.Alt & data.Alt <= .9,:)
filteredData = 7x3 timetable
Time Lat Lon Alt ____________________ _______ _______ _______ 01-Jan-2025 14:21:21 0.54884 0.38806 0.58854 02-Jan-2025 11:09:16 0.10131 0.83326 0.86847 03-Jan-2025 07:54:48 0.76736 0.1542 0.12701 04-Jan-2025 10:37:43 0.33832 0.57193 0.88036 08-Jan-2025 19:32:28 0.36297 0.99531 0.56691 09-Jan-2025 20:22:50 0.8221 0.91991 0.52526 10-Jan-2025 08:54:33 0.18142 0.39746 0.37439

Categories

Find more on Data Import from MATLAB 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!