If I run the script:
z=1:1000;
parfor i=1:numel(z)
zfun(z(i))
end
which calls the function:
function zfun(z)
for j=1:50
for k=1:100
myz=z*10+myz*k;
end
end
save(fullfile('filepath', num2str(myz)), 'myz');
end
I get an error of Undefined function or variable 'myz' for the save command.
Any ideas how to fix this?

댓글 수: 2

Walter Roberson
Walter Roberson 2017년 1월 20일
Which MATLAB version?
My spidy-senses are suggesting you might be using R2015b or R2016a but not R2016b ?
JohnDylon
JohnDylon 2017년 1월 20일
R2016b trial for Linux. (No error for 2014a on PC though!)

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

 채택된 답변

Walter Roberson
Walter Roberson 2017년 1월 20일

0 개 추천

You do not initialize the variable in the zfun loop.

댓글 수: 5

JohnDylon
JohnDylon 2017년 1월 21일
Initializating "myz=[]" outside of the zfun loop and passing it through the function as zfun(z(i), myz) done the job. Thanks again!
Walter Roberson
Walter Roberson 2017년 1월 21일
There should not be a need for that; you should be able to just initialize it to 1 inside zfun.
Initializing to [] is going to result in a lot of multiplying with the empty matrix, which will not be productive.
Then you say
function zfun(z)
myz=1;
for j=1:50
for k=1:100
myz=z*10+myz*k;
end
end
save(fullfile('filepath', num2str(myz)), 'myz');
end
is better to do?
Walter Roberson
Walter Roberson 2017년 1월 21일
Yes, that would have less overhead. However, due to finite precision of floating point numbers, the result of the above is going to be -inf below about -1/17 and +inf above it. The zero is at about -0.058197670686932642438500200510901 . If you were trying to find the zero, it would be much more effective to use the symbolic toolbox.
JohnDylon
JohnDylon 2017년 1월 22일
Thank you for all valuable guidance.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

태그

질문:

2017년 1월 20일

댓글:

2017년 1월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by