필터 지우기
필터 지우기

Strange behaviour appears like Matlab bug

조회 수: 1 (최근 30일)
Tomas Iesmantas
Tomas Iesmantas 2013년 4월 8일
The problem: in each iteration of the loop, a string is created and the converted to the function str2func, e.g. myfunc=str2func(string1); However, after several thousand iterations (not at particular, but seems like random event) myfunc result is the function used some iterations ago, while str2func(string1) as executed directly in matlab command window, returns the function correctly. But if I execute the line myfunc=str2func(string1), myfunc is not the same as str2func(string1) would produce. I am really baffled, seems like out of nowhere - it's like the line "f=1" would return 3 and not 1. Is it a memory thing or what? I have no idea on this.
  댓글 수: 3
Tomas Iesmantas
Tomas Iesmantas 2013년 4월 8일
Hi, The code itself is too long to be of any use here. Would need to spend quite a long time, to describe how it works. I do not create lots of handles, instead I overwrite the old with the new one.
Cedric
Cedric 2013년 4월 8일
편집: Cedric 2013년 4월 8일
Does the pool of these function names contain only your own functions, or does it contain built-ins? If there are built-ins, which ones?
If you had for example an array named funcIds and one of the function names would be funcIds as well, you would use the array by evaluating funcIds(k) until the call to str2func('funcIds'), and the next evaluation of funcIds(k) would be a function call.
Jan's comment/answer is certainly motivated by the fact that we almost never see cases that justify your approach; if you are sure that it is the only possible one, fine, but if you have a doubt we could also discuss the approach.

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

답변 (1개)

Jan
Jan 2013년 4월 8일
편집: Jan 2013년 4월 8일
if I execute the line myfunc=str2func(string1), myfunc is not the same as str2func(string1) ...
This happens, when there are two functions with the name "string1" in different folders and during the execution the current folder changes. Then you can get two different replies, and both are true.
A similar effect can appear, when a function is read and modified afterwards. Then the function is only reloaded after a clear(string1) or clear('all').
Obviously creating a large number of function handles and perhaps creating M-files dynamically (?) is prone to errors and therefore a bad programming practize.
  댓글 수: 1
Tomas Iesmantas
Tomas Iesmantas 2013년 4월 8일
First of all, neither of the issues that you mentioned are present in my settup - no change of working folder occur and no modification of the function myfunc is made after the conversion from string (myfunc=str2func(string1)). And clear myfunc does not help. It just keeps mistakenly attaching the wrong function. However, at the next iteration, everything is ok - iterations are independent of each other.
Second, I do not creat M-files dynamically. I just function handle ("@"). And since at each iteration I do not no a priori the exact structure of the function "myfunc" I see no other way on how to solve the problem I am working on.

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

카테고리

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