How do I assign an index value to a function output?

11 次查看(过去 30 天)
Using R2014b. I have a function called within a for loop which returns lots of outputs. I want to assign an index value for each output of the function as the loop runs. Something like:
for ind = 1:n
[output1(ind),output2(ind), ...] = function(inputs)
end
This doesn't appear to work (results in an error). Is there an easy way to code this without doing:
output1(ind) = output1;
output2(ind) = output2;
for each variable after the function call?
  3 个评论
Stephen23
Stephen23 2016-1-3
Indexing into an output argument works without error:
>> for k = 1:3, [R(k),C(k)] = size(ones(1,k));, end
>> R,C
R =
1 1 1
C =
1 2 3
If you want help finding your syntax error then you need to actually tell us what the error message is, and show us your code.
Art
Art 2016-1-3
Walter, there are multiple types of outputs (numeric, text, could be a cell or not) and they could be empty. Perhaps that is my problem. Stephen, thanks for the example. I see this works in that case. I worked around my problem by recoding the loop within the function itself and returning indexed outputs. Thanks for the answers!

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2016-1-3
You cannot assign an empty result into an indexed location, not unless you are using {} indexing on a cell array. Also remember that strings are row vectors of characters, not single locations.
You might need
for ind = 1:n
[output1{ind},output2{ind}, ...] = function(inputs)
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by