Conversion to double from struct is not possible.

조회 수: 25 (최근 30일)
Bert Taekels
Bert Taekels 2017년 11월 9일
편집: Guillaume 2017년 11월 9일
To make a visualization of a function inside of a loop i used getframe. If i don't specify a number after the variable it only saves the last frame so i used this code
OBS(counter) = getframe;
Where OBS will be later used in movie(OBS) and the counter is the same counter as used in the loop. In a muck up code, this worked perfectlty. But in my main code it gives the error message: Conversion to double from struct is not possible. I know OBS here is the construct. Counter can't be it is equal to 1 ( i can see that in the workspace). OBS is a global, but that shouldn't be the issue.
the code where it works is
counter = 1;
for k = 1:16 plot(fft(eye(k+16))) axis([-1 1 -1 1]) PAR(counter) = getframe; counter = counter +1; end
pause figure movie(PAR)
i'm stuck on this, thanks for any tips or help Bert
  댓글 수: 3
Bert Taekels
Bert Taekels 2017년 11월 9일
편집: Bert Taekels 2017년 11월 9일
I believe the problem is, that i create the global before i use it ( which is the logical way). This way the global is defined as a double where the getframe function expects a construct and gives the error. I should then either clear the global (which worked on the function level of my code, but gave other issues) or define the global as construct to begin with. I have yet to find out how that has to be done, but i think that will solve my issues
Stephen23
Stephen23 2017년 11월 9일
편집: Stephen23 2017년 11월 9일
"OBS is a global, but that shouldn't be the issue."
Famous last words. Avoid using globals. Pass variables properly.

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

답변 (1개)

Guillaume
Guillaume 2017년 11월 9일
편집: Guillaume 2017년 11월 9일
As has been said and will keep being said, using globals is a mistake. While it may work fine for now with your simple code, it will bite you back as the code becomes more complex.
Anyway, your problem is indeed that you cannot assign a structure to something by indexing into it if that something is not a structure with the correct field. You can solve your issue by declaring that something as an array of empty structures with the correct fields:
numelem = 16; %or whatever number of elements you're predeclaring
OBS = struct('cdata', cell(1, numelem), 'colormap', cell(1, numelem)); %predeclare struct array
%...
for counter = 1:numelem
OBS(counter) = getframe; %will not error
end

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by