Read row of data from excel file and step/iterate through each row when program is looped
Mostra commenti meno recenti
Good day,
Trying to read 1 row of data each time program loops from excel file. Excel contains 2 columns of data. Each time my program loops, therefore updating values of Data1 and Data2.
Below extract of code reading imported excel file;

Disp('Currently importing from excel raw data file')
Data = xlsread('Data');%reading excel file
newData1 = Data(:,1);Reading column 1
newData2 = Data(:,2);%Reading column 2
Objective is to update equations as new data is measured, measuring using A1 and B1 1st loop, for instance.
measuring A2, B2, - 2nd iteration of programm/loop, etc....
Above is finite set of excel data, for simulation.
Thanks in advance,
kind regards Vincent
6 Commenti
Allen
il 7 Gen 2020
Vincent,
It is not clear what you are trying to do with the data in columns after you assign them to the variables newData1 and newData2. Can you provide examples of the row-wise (looped) code that you have tried to perform after assigning values to the newData variables?
Guillaume
il 10 Gen 2020
Comment by Vincent Hodnett mistakenly posted as an answer moved here (Please use comments not answers to comment)
Hello Meg, Allen,
Thanks for your help, and apologies from deviating from heading. End objective is that trying to read in Data 1, Data 2 in step.6 of the following code, where the system receives a new measurement. And when get to the end of code, step.9 updates the variable Xk_p and Pk_p, to the 'Step.2 - Predicted State Matrix' and 'Step.4 - Predicted Process Covariance' equations respectively. Little lost as how to iterate/loop the program, until it reads all Data1 and Data 2 columns. Where column A is meters and column B is meters/second:

Below is the code which trying to implement, object is Kalman filter algorithm, code follows written calculations, assuming initial variables.
kind regards Vincent
irow = 1;
Data = xlsread('gliderdata.xlsx','Sheet1',['A' num2str(irow) ':B' num2str(irow)]);
while ((~isempty(Data))
irow = irow + 1;
Data = xlsread('gliderdata.xlsx','Sheet1',['A' num2str(irow) ':B' num2str(irow)]);
%1.Initial State Variables;
disp('1.Initial State Variables and Constants;')
T = 1%Duration of Algorithm
A = [1 T; 0 1]%A matrix
B = [0.5*T^2; T]%B matrix
g = [-9.8]% Gravity constant
w = randn;%Random variable
%2.Predicted State Matrix Calculation;
disp('2.Predicted State Matrix Calculation')
Xkp = A*Xk_p+B*g+w;%Xk-1 is updated at each iteration of algorith
disp('Predicted State Variable with random noise added:')
disp(Xkp)
%3.Initial Process Covariance matrix;
disp('3.Initial Process Covariance matrix')
DeltaPY = 1.5;
DeltaPV = 0.5;
Pk_p = [DeltaPY^2 0; 0 DeltaPV^2];
disp('Initial Process Covariance Matrix:')
disp(Pk_p)
%4.Predicted Process Covariance matrix;
disp('4.Predicted Process Covariance matrix')
Pkp = A*Pk_1*A'+w; %First Pk_1 values comes from step.3
disp('Predicted Process Covariance Matrix with random noise added:')
Pkp = diag(diag(Pkp));
disp(Pkp)
%5.Calculation of Kalman gain;
disp('5.Calculation of Kalman gain')
disp('Kalman Gain with random noise added:')
K = Pkp *[1 0; 0 1]/([1 0; 0 1]*Pk_1*[1 0; 0 1] + [2^2 0; 0 0.28^2])
%6.New measurement;
disp('6.New measured data')
NewMeas1 =[Data(1); Data(2)];
NewMeas2 = NewMeas.';%Transposed to Column vector, 2x1 matrix
%Disp('Importing from excel raw data file')
%rawData = xlsread('GliderRawData');
%newAltitude = rawData(:,1);
%newVelocity = rawData(:,2);
%7.Calculation of Current State Matrix
disp('7.Calculation of Current State Matrix')
disp('Calculation of Current State Matrix with random noise added:')
Xk_p = Xkp +K*([NewMeas2]-[1 0 ; 0 1]*[Xkp])
%8.Updating the Process Covariance Matrix
disp('8.Updating the Process Covariance Matrix')
disp('With random noise added:')
Pk_p = [1 0; 0 1]-(K*[1 0; 0 1]*Pkp)
%9.Current State becomes Previous State
disp('UPDATED - Predicted State Variable with random noise added:')
%% Xk-p
disp('UPDATED - With random noise added:')
%% Pk-p
disp('9.Current State becomes Previous State')
end
Guillaume
il 10 Gen 2020
I'm a bit confused by the premises of the question.
Does the excel file change while the matlab code is executed? I.e some instrument rewrite the excel file while the code runs? If so, isn't there a more efficient/reliable method of getting the instrument data?
Vincent Hodnett
il 10 Gen 2020
Guillaume
il 10 Gen 2020
So, if the file is fixed why does it need to be read in a loop?
Sorry, I'm a bit confused as to what you have as input and what you want as output.
Vincent Hodnett
il 11 Gen 2020
Modificato: Vincent Hodnett
il 11 Gen 2020
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Spreadsheets in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!