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

5 visualizzazioni (ultimi 30 giorni)
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 Commenti
Stephen23
Stephen23 il 3 Gen 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 il 3 Gen 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!

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 3 Gen 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

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by