saving each iteration of Nested for-loops in a matrix

조회 수: 8 (최근 30일)
Antonio Sereira
Antonio Sereira 2015년 2월 7일
댓글: Stephen23 2015년 2월 8일
Hi!
I have a nested for-loop (f. ex.: ii=1:length(x1), jj=1:length(x2),...) in which I call my function file. The function has several outputs. Now I want to save the outputs for each iteration and so create a matrix. Additionally I plot in the function file also and save it as png file. This should be saved as with the right iteration name (so that I lnow that this figure belongs to x1=4,x2=3,x3=2, x4=1)
How I can do this?

답변 (2개)

Geoff Hayes
Geoff Hayes 2015년 2월 7일
Antonio - for any iteration you have a png image and several variables. Try saving all of this to a structure like
data.x1 = 4;
data.x2 = 3;
data.x3 = 2;
data.x4 = 1;
data.img = ...;
Then save each of these structures to an array (or cell array) that is updated at each iteration of your inner loop. Using a structure allows you to keep all relevant info together, and putting it all in an array (cell or otherwise) keeps all of the output from each iteration together.

Antonio Sereira
Antonio Sereira 2015년 2월 8일
Hi!
The problem is that I have to save each output in a matrix like the first column of matrix A (correspond to output y1) are the values for m=1. Second coulumn for m=2. The problem is my function has several outputs and I called all the outputs "Results=[y1,y2,y3,...]. And now I would like to do something like
"A_1(:,count)=Result(...)".
You understand what I mean?
  댓글 수: 1
Stephen23
Stephen23 2015년 2월 8일
If count is your loop variable and each of yN is scalar, then doing
A_1(:,count) = Result;
will allocate your vector Results to a new column of A_1, thus storing them. Try it!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by