Loop input variables into function to get the various output in table
1 次查看(过去 30 天)
显示 更早的评论
Hello!
I have a function for which i have some variables as input that create output as follows:
function(A,B,C,D,E,F,G) - So I input varibles A thru G and get the below output X thu Z
for i = 1:varout
switch i
case 1
varout{1} = X;
case 2
varout{2} = Y;
case 3
varout{3} = Z;
end
I have for example the following input variables I want to pass thru the function all at once for me to get a table with all the output variables given the input.
A B C D E F G X Y Z
1 2 1 2 6 7 1 =
3 1 1 8 7 4 10 =
1 5 1 2 6 3 8 =
Appreciate all the help in this matter! Thanks so much!
8 个评论
Adam Danz
2022-7-18
That should be easy to implement. If those are your variable names, then that's exactly the code you would use to produce those outputs. But I must admit, I am still quite uncertain of the goal.
采纳的回答
Voss
2022-7-18
Something like this maybe?
% your initial table
input_table = array2table([ ...
41 62 92 17 3 7 -2; ...
41 61 92 18 4 7 -2; ...
41.5 62 92 6 3 7 -2; ...
],'VariableNames',{'A' 'B' 'C' 'D' 'E' 'F' 'G'})
% convert to a cell array
inputs = table2cell(input_table)
% pass each row of inputs to your function, and
% collect the outputs in another cell array
N = size(input_table,1);
outputs = cell(N,3);
for ii = 1:N
[outputs{ii,:}] = your_function(inputs{ii,:});
end
outputs
% convert the outputs to a table
output_table = cell2table(outputs,'VariableNames',{'X' 'Y' 'Z'})
function [X,Y,Z] = your_function(A,B,C,D,E,F,G)
X = A+B+C+D+E+F+G;
Y = X / 2;
Z = X+Y;
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!