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