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

2 vues (au cours des 30 derniers jours)
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.
  1 commentaire
Walter Roberson
Walter Roberson le 6 Sep 2023
See readtable and rowfun ... though if you are specifically told to use a loop then you will instead want to use a for loop with height

Connectez-vous pour commenter.

Réponse acceptée

Clay Swackhamer
Clay Swackhamer le 6 Sep 2023
Modifié(e) : Clay Swackhamer le 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
close all
%% 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));
%% Now, rename the output and add it back to the table
T.ECEF = output';
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.

Plus de réponses (1)

Dyuman Joshi
Dyuman Joshi le 6 Sep 2023
Modifié(e) : Dyuman Joshi le 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.


En savoir plus sur Loops and Conditional Statements 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