I've got a simple problem, I run a parfor loop inside which i call a function which return some variables, the problem is that some of this variables does not show in the workspace.

 채택된 답변

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020년 4월 19일

0 개 추천

You have an error in your mont_carlo inputs, it should be
[x,neigh,E,M]= monte_carlo ( N, T, iter,init )
or you should delete the line
x = init ;
Besides this what exactly is the problem you're having? E and M should be showing in the workspace, since you gave it index to them. x and neigh are replaced in every loop in a random order, so no consistent result can be expected from them.

댓글 수: 4

andrea
andrea 2020년 4월 19일
Yes sorry for the error x = init, iI have noticed it.
But thank for the answer the problem was x and neigh.
Because I should find a way to imagesc(x) at the end of each iteration, i.e every time i run monte_carlo.m i would like to have x to plot it.
Do you think it is possible ?=
Sure. You can either plot in the loop as:
parfor...
figure,imagesc(x),title(['T: ',num2str(T_span(i))])
end
or save the x and neigh variables in a cell, so you can retrieve all of them after the parfor loop:
[x{i},neigh{i},E(i),M(i)] = monte_carlo(N,T_span(i),iter,init) ;
andrea
andrea 2020년 4월 19일
It works fine, could you briefly explain me why does it work with cell ?
When you have only one single value, those can be saved in a vector for each iteration, as the case with E and M. If you have a matrix, vector or anything different than a single number, it is not possible to attribute it's value to a single vector entry, so you have to use data structures that can handle it. Cell is just one of them, where you can save basically anything and then retrieve it with only the index.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

제품

릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by