How to create exportable table from outputs of function iterated using for loop?
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Stephen23
2021-7-22
编辑:Stephen23
2021-7-22
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:
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!