How to save data from for loop into cell without rewriting?

Dear all,
I have this code:
A ={5,2};
c = cell(size(A))
v = 0:0.1:1
idx = 1;
for j = 0:0.5:1
v(idx) = j;
['x-' num2str(j) '.^2+ (y- 0.5) .^2<0.1^2, ''x,' 'y,' 'z'];
select_fcn = inline('(x-j).^2+(y-0.5).^2<0.1^2','x','y','z');
img_2.elem_data = 1 + elem_select(img_2.fwd_model, select_fcn);
idx = idx + 1;
figure
show_fem(img_2);
vh = fwd_solve(img_1);
vi = fwd_solve(img_2);
img_3 = inv_solve (imdl,vh,vi);
figure
show_fem(img_3);
c{k,1}=j;
c{k,2} = idx
end
I would like to save data (j and idx) to cell, but without rewriting. Like in my case in Comand Window:
c =
[] []
[] []
[] []
[] []
[4] [0.5000]
c =
[] []
[] []
[] []
[] []
[4] [1]
Why didn´t fill whole cell array? Why was filled only one line and still be rewritten?
I have no idea.
Thank you for any ideas.

댓글 수: 3

How does k get its value?
This is my mistake...K has no value. I just wanted to make sure, that data (j and idx) wrote down the first and second columns. But I don´t know how to do it.
Note that you should not use i of j as your loop variables, as these are the names of the inbuilt imaginary unit .

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

 채택된 답변

Star Strider
Star Strider 2015년 1월 23일
You increment ‘idx’ by 1 in each iteration, so I would add one line here to define ‘k’:
k = idx;
c{k,1}=j;
c{k,2} = idx;
That should solve your problem.

댓글 수: 4

Thank you, this solution really solve my problem.
But I have another problem - all of iteration steps are saved in one image. I would like to display function
select_fcn = inline('(x-j).^2+(y-0.5).^2<0.1^2','x','y','z');
with varying argument (j) after each iteration step to display the appropriate images.
Thank you.
My pleasure!
I do not understand what you want to do with your inline function. (There is no ‘z’ in it.)
I would replace it with the anonymous function:
select_fcn = @(x,y) (x-j).^2+(y-0.5).^2<0.1^2;
I am not certain what you mean by ‘image’, either. If you want to print out this line ['x-' num2str(j) '.^2+ (y- 0.5) .^2<0.1^2, ''x,' 'y,' 'z'] at every step,
use the fprintf function:
fprintf(1, '\t\nx-%d.^2 + (y- 0.5).^2<0.1^2, x=%f, y=%f, z=%f\n', j,x,y,z);
I have no idea what you are doing or what you want, so this is simply a guess.
Thank you for your answer.
I try to explain what I mean.
I have this inline function (('(x-j).^2+(y-0.5).^2<0.1^2','x','y','z'), where j is variable (j= 0:0.5:1). So my function for example looks like (('(x-0.5).^2+(y-0.5).^2<0.1^2','x','y','z'). And I want to this function display in my figure (image = img_2) so it displays this fleck.
But in my for loop, there is something wrong, because there are all flecks in one figure (image = img_2) so it displays this
Do you have any idea?
Thank you very much for your answer.
I honestly have no idea. It would be best for you to post this as a new Question. Attach the file for the code that is producing the figure, so we can experiment with it.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2015년 1월 23일

댓글:

2015년 1월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by