Jake
Jake

Reputation: 37

Creating matrix of output from script (matlab)

I have a script something like the following:

for 
(do something)
end

And also outputs that use data output from the loop (which change each time -- when the script is run):

A = 1
A = 1.5

Etc.

I am looking to store this output that changes each time into a matrix. Is this feasible?

for number of iterations
(Call script)
end
Output to excel

The reason I want to store the data into a matrix is to be able to output all the answers (for several iterations) into Excel at once.

Edit: To give a better picture of what my output looks like it is something like this

Output = [rand() rand() rand(); rand() rand() rand()];

then I use this to create a new variable:

var = Output(1,1)./Output(2,1); 

each time I run the script the answer changes. This new answer each time, is what I am looking to save in a matrix. Hope that clears things up.

Upvotes: 0

Views: 1462

Answers (2)

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

Assuming that var is the thing you want to put in the matrix after each iteration, I suggest the following: Add another for loop around your code, for example loop over i, then in the end do not assign the value to var, but to var(i).

Depending on your output you need to choose the variable type of var, for example cell or matrix.

Upvotes: 1

gevang
gevang

Reputation: 5014

Depending on the type of output/outputs from each loop you can, trivially, save intermediate results in one out of many MATLAB data structures(randn is used in the following as a sample of "do something"):

nIterations = 10;

% scalar output
A = zeros(1, nIterations);
for n=1:nIterations
    A(n) = randn;
end

% matrix ouput of possibly changing size
B = cell(1, nIterations);
for n=1:nIterations
    B{n} = randn(1, n+1);
end

% matrix output of fixed size
C = zeros(3, 3, nIterations);
for n=1:nIterations
    C(:,:,n) = randn(3, 3);
end

Upvotes: 3

Related Questions