Filter löschen
Filter löschen

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

11 Ansichten (letzte 30 Tage)
Art
Art am 3 Jan. 2016
Beantwortet: Walter Roberson am 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 Kommentare
Stephen23
Stephen23 am 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 am 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!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by