Effacer les filtres
Effacer les filtres

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

11 vues (au cours des 30 derniers jours)
Art
Art le 3 Jan 2016
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 commentaires
Stephen23
Stephen23 le 3 Jan 2016
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 le 3 Jan 2016
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!

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 3 Jan 2016
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

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by