필터 지우기
필터 지우기

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

조회 수: 11 (최근 30일)
Art
Art 2016년 1월 3일
답변: Walter Roberson 2016년 1월 3일
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