Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Plot animation Effcientcey and double to struct error

Asked by Lawson Hoover on 8 Dec 2012

I am trying to animate a plot. I have got it to work but I seems to moving a tad slow, and Also when I allocate the frames into a variable M with getframe, the M gets a red underline saying that I should preallocate for speed. I have been trying to do this but I get is the error:

   The following error occurred converting from struct to double:
  Error using double
  Conversion to double from struct is not possible.

I have no idea how to fix this. The code I am using is below:

   plot(x,y,'--r','LineWidth',3);  % original position
  xlabel('Beam Length (in.)','Color','g');
  ylabel(bstr,'Color','g');
  ylim([-(Beam.y(10)) max(Beam.y)]);
  title(astr,'Color','y','fontweight','b');
  set(S.ax,'YDir','reverse');
  set(S.ax,'XGrid','on');
  set(S.ax,'YGrid','on');
  y =0;
  z = 0;
  hold on 
  clear M
  M = zeros(1,100);
  ht = plot(y,z,'-b','linewidth',2); % new position
  for i = 1:100
    if i == 1
        set(ht ,'XDataSource','Beam.x')
        set(ht,'YDataSource','Beam.y')
    end
  set(ht,'XData',Beam.x(1:i))
  set(ht,'YData',Beam.y(1:i))
  M(i) = getframe(gcf);   % this says that I shoudld preallocate it but when I do I get an error
  end
  legend('Without Deflection','With Deflection');
  hold off

0 Comments

Lawson Hoover

Products

No products are associated with this question.

1 Answer

Answer by Walter Roberson on 8 Dec 2012

getframe() returns a movie frame, which is a struct. You cannot store a struct into a scalar double location such as M(i) where M has been initialized to zeros(1,100)

http://www.mathworks.com/help/matlab/ref/getframe.html

What are you going to do with the frames after you get them? If you are going to create a video, then see the documentation for VideoWriter for an example of how to do it.

3 Comments

Lawson Hoover on 8 Dec 2012

Well I was trying to store the frams in one variable and then be able to call up a particular slide, or position, in the movie with a slider or edit box.

Walter Roberson on 8 Dec 2012
M(100) = struct('cdata',[], 'colormap', []);  %preinitialization
Alexander on 24 Apr 2013

Thank you so much! That error was had me stuck all day!

Walter Roberson

Contact us