MATLAB Answers

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

1 ビュー (過去 30 日間)
Rame Putris
Rame Putris 2021 年 7 月 22 日
編集済み: Stephen 2021 年 7 月 22 日
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
Stephen 2021 年 7 月 22 日
編集済み: Stephen 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:
  1 件のコメント
Rame Putris
Rame Putris 2021 年 7 月 22 日
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!

サインインしてコメントする。

その他の回答 (0 件)

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by