MATLAB: Run a function a few times with always different data? How to

forfunction automatizationloop

Hi there,
I have the following problem: 1. I read a dataset (no headers or strings) from excel in by xlsread. The first column is a date and the following 5 columns are numerical. The dataset contains 5 rows. With xlsread I get a 5×6 matrix.
2. What I want to do now is to run my function with the data of the first row. Save the solution in a csv file. Then matlab should take the second row of my data, run the function again and save it in the same csv. This routine should be performed until matlab reaches row 5.
3. The csv file should always contain the date, followed by the obtained solution from the function. That must be done for each row.
For simplicity the function could be "func = a+b+c+d+e"
How can I do it?
Thanks in advance boulala

Best Answer

Use a for loop to loop through each row, then save the result as a matrix.
Hypothetical question, so hypothetical solution:
data = xlsread( ... ); % ‘data’ = (5x6) matrix
func = @(vars) var(1) + var(2) + var(3) + var(4) + var(5);
for k1 = 1:5
result(k1,:) = [data(k1,1) func(data(k1,2:6))];
end