For loop that executes data from an excel file, your mission should you choose to accept it

2 views (last 30 days)
I am starting this with an apology, I am extremely new to Matlab and though I have taken 3 coding classes (Python and C) I have recently learned I know nothing. I have been attempting to complete the following for weeks now and I am losing all sanity though I am sure it's simple for those who speak and understand code.
Here's the mission:
I have been instructed to make a program that accepts/reads latitude,longitude, altitude, roll, pitch, yaw data from an excel file, and then uses that data in the formulas I will provide below to output, for now, simple ECEF coordinates. It is suppose to run a for loop that cycles each value (row 1 column1) of the data (if its necessary to the formula) through the equation and outputs the now transformed coordinate.
Yes I am aware that matlab has a geodic to ECEF function already but sadly I am not allowed to use it along with any other imeded functions that may seem like and obvious fix.
I need help understanding how to write a simple for loop that executes that... if I did not explain well even an example slightly similar to that with excel data will be more than helpful. I will greatly appreciate any help.

Accepted Answer

Clay Swackhamer
Clay Swackhamer on 6 Sep 2023
Edited: Clay Swackhamer on 6 Sep 2023
Here is some code that reads a simple excel table (attached) and calculates a new variable based on the function "ECEF_function". The code uses a for loop to go through the table one row at a time, and then calculate a new variable called "output" based on the data in the same row of the table:
%% calculate_on_excel_table
%% Setup
clear
close all
clc
%% Get data
T = readtable('excel_data.xlsx')
%% Define an equation for the calculation
% Say it takes two inputs (x and y)
ECEF_function = @(x,y) x^2 + y^2
%% Loop through the table one row at a time
% Calculate some output based on the data in the same row
for i = 1:height(T)
output(i) = ECEF_function(T.latitude(i), T.longitude(i));
end
%% Now, rename the output and add it back to the table
T.ECEF = output';
T
Dyuman Joshi is right, you should look into preallocating and vectorizing this code. But hopefully this helps you visualize what is going on and get started.

More Answers (1)

Dyuman Joshi
Dyuman Joshi on 6 Sep 2023
Edited: Dyuman Joshi on 6 Sep 2023
"I need help understanding how to write a simple for loop that executes that"
A basic idea will be -
1 - Read the data via readmatrix or readtable
3 - Go through the data via a for loop, convert the data using formulae (available by a simple search on the internet, if you don't already know them) and store the data in the preallocated matrix.
You can use vectorization to obtain the output as well, but for now, the above method should suffice.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!