# How to create exportable table from outputs of function iterated using for loop?

1 view (last 30 days)
Rame Putris on 22 Jul 2021
Edited: Stephen on 22 Jul 2021
I have a working function which I have created which takes 6 inputs and puts out 4 outputs which are Rg, GPR, Es and Em. I am trying to iterate the function using a for loop which starts at a value of 50 and goes to 500 in steps of 10. For each index value x, I would like to populate a row of a table using 5 columns for the 4 outputs plus the itteration index.
Here is my code:
for x = 50:10:500
Design2 = gnd(144,120,6,5,22,x); % Calls my function
table1(x,:) = [x Rg GPR Es Em] % Create table from outputs of Design2 filling a multi-row by 5 column table
end
I have tried the cell method and some other table methods but I cannot get it to work. The code I have above says it does not know the variable Rg. How can I connect the output of the gnd function into each of the table columns?
I appreciate your time and guidance with this. I have searched google and matlab answers to no end and have not yet found a way to get it working.
Thank you.

Stephen on 22 Jul 2021
Edited: Stephen on 22 Jul 2021
If your function has four outputs then you need to call it with four outputs. This is explained in the introductory tutorials:
Assuming that the function outputs are scalar numerics of class double then you could use one matrix, e.g.:
V = 50:10:500;
N = numel(V);
M = nan(N,5);
for k = 1:N
x = V(k);
[Rg,GPR,Es,Em] = gnd(144,120,6,5,22,x);
M(k,:) = [x,Rg,GPR,Es,Em];
end
Otherwise you will need to use a table or a cell array, e.g.:
V = 50:10:500;
N = numel(V);
C = cell(N,5);
for k = 1:N
x = V(k);
C{k,1} = x;
[C{k,2:5}] = gnd(144,120,6,5,22,x);
end
Note how I used a comma-separated list to assign the four outputs into one cell array:
Rame Putris on 22 Jul 2021
Wow thank you so much! It worked just as I had hoped. I will work on plotting the numbers now but finally this part is accomplished.
I will look into the references you have mentioned for future learning, I appreciate your help Stephen Cobeldick!

R2019b

### Community Treasure Hunt

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

Start Hunting!