Local functions. Appending results of looping to previous variable; creating separate variable for each loop.

6 views (last 30 days)
I have a local function that returns two variables, say [icke,data]=localfunction(x). I am using a for loop to apply the local function several times.
for i=1:10
[icke,data]=localfunction(x)
end
Want I want to do is for each loop of the local function for icke, I want the result to be appended to the bottom of the previous icke. Right now the above removes and replaces the previous each time.
For the second variable data, I would like a new variable to be created for each one, ideally, so we have data1, data2, ..., data10.

Answers (1)

Star Strider
Star Strider on 21 Nov 2018
Edited: Star Strider on 21 Nov 2018
Subscript the outputs. If they are scalars, use:
for i=1:10
[icke(i,:),data(i,:)]=localfunction(x)
end
If they are vectors, or if there are different size outputs, use cell arrays:
for i=1:10
[icke{i},data{i}]=localfunction(x)
end
Please never name variables dynamically. Simply refer to them by thier subscripts.
Also, you may want to subscript the inputs as well:
for i=1:10
[icke(i,:),data(i,:)]=localfunction(x(i))
end

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!