Videowriter, issues with making a video

조회 수: 2 (최근 30일)
Lissa
Lissa 2021년 1월 3일
답변: Walter Roberson 2021년 1월 5일
Hello.
We have some troubles with making a video by using the function VideoWriter. Are we not referring to it correctly? Thanks!
This is our code of the function:
function [AnimationFigure] = SimulationVraag5Function(TDnumber,condition,trialnumber,tibialength,x_knee,y_knee)
% SimulationVraag5Function
% SimulationVraag5Function is a function that will create a plot to visualize
% the pendulum test in a stick figure
% Input arguments
% TDnumber: fill in number of subject, f.e. for TD3 fill in '3'
% condition: fill in '1' if condition is HR, fill in '0' if condition is MR
% trialnumber: fill in number of trial HR/MR, f.e. for MR5 fill in '5'
% tibialength: fill in length of tibia of the subject [in cm]
% x_knee: fill in x-co of the knee (we considered the knee to be a static
% point, the tibia was able to move during the pendulum test) [in cm] (fill in a value that fits in coordinate system of -70 by 70)
% y_knee: fill in y-co of the knee [in cm] (fill in a value that fits in coordinate system of -70 by 70)
% Output arguments
% AnimationFigure: an animation figure of the knee during pendulum test will be made, we considered other segments to be static at a chosen coordinate by us
SimulationVraag5Video = VideoWriter('SimulationVraag5Video.avi');
open(SimulationVraag5Video);
% IMPORT DATA (for this exercise we need IK data)
startpath ='/MATLAB Drive/Assignment_V2/MBEXAMEN/EXAMEN/Data';
Datapath = [startpath '/Data'];
addpath '/MATLAB Drive/Assignment_V2/MBEXAMEN/EXAMEN'
% right/left subjects HR
if condition == 1 % this is the first main loop, condition = 1 stands for HR trials
for i = TDnumber % TDnumber was filled in (see function), we will retrieve all the files for specific TDnumber
dataFolder = [startpath '/TD' num2str(i)];
dataFile = fullfile(dataFolder, '/IK/HR*.mot');
pathwayIK_HR = dir(dataFile);
% loop left subjects HR
if i == 3 | i == 5 | i == 7 | i == 9 | i == 14 % making a distinction between left/right leg tested subjects, 3/5/7/9/14 are left leg tested
x_hip = x_knee+30 % since the left leg is tested, we want the x-coordinates of the other joints (hip, shoulder, head) to be positive (will be to the right of knee x-co), so we add a number to the x-co of the knee (to get a relative visual representation)
y_hip = y_knee+10 % we want the y-coordinates of the other joints (hip, shoulder, head) to be positive (will be above knee y-co)
x_shoulder = x_knee+35;
y_shoulder = y_knee+50;
x_midhead = x_knee+35;
y_midhead = y_knee+60;
dataFolder = [startpath '/TD' num2str(i)];
for k = trialnumber % getting the files for the specific HR trial
% data HR
IKHRdata = importdata(fullfile(pathwayIK_HR(k).folder,pathwayIK_HR(k).name));
DataHRlinks = IKHRdata.data(:,strcmp('knee_angle_l',IKHRdata.colheaders)); % get the knee_angle_l because left leg testings
TimeHRlinks = IKHRdata.data(:,strcmp('time',IKHRdata.colheaders));
kneeangle = IKHRdata.data(:,strcmp('knee_angle_l',IKHRdata.colheaders))
for a = 1:100:length(kneeangle) % we chose steps of 30 between the rows, otherwise the running would take a long time
% computing the lengths of the triangle: femur length (length between knee and hip) and length between hip and ankle
lengthbetweenkneehip = sqrt(((x_knee-x_hip)^2) + (y_knee-y_hip)^2) % computing distance between two coordinates (co hip and co knee are known)
lengthbetweenanklehip = sqrt(tibialength^2 + lengthbetweenkneehip^2 - (2*tibialength * lengthbetweenkneehip * cos(kneeangle(a)))) % law of cosines in a triangle
lengthbetweenanklehipsquared = lengthbetweenanklehip^2 % to get rid of the root square on the other side of the equation
% vgl1 = (xhip-xankle)^2 + (yhip-yankle)^2 - lengthbetweenanklehipsquared
lengthbetweenkneeankle = tibialength % this is known (requested in the funtion as input argument)
lengthbetweenkneeanklesquared = tibialength^2 % to get rid of the root square on the other side of the equation
% vgl2 = (xknee-xankle)^2 + (yknee-yankle)^2 - lengthbetweenkneeanklesquared
syms x_ankle y_ankle
E = [((x_hip-x_ankle)^2 + (y_hip-y_ankle)^2 - lengthbetweenanklehipsquared == 0), ((x_knee-x_ankle)^2 + (y_knee-y_ankle)^2 - lengthbetweenkneeanklesquared == 0)]; % two equations are set to zero, two equations so the two unknown variables of x_ankle and y_ankle will be solved
S = solve(E,x_ankle,y_ankle)
x_ankle = S.x_ankle % there are two possible values for x_ankle
y_ankle = S.y_ankle
x(1) = x_knee
x(2) = x_ankle(1)
y(1) = y_knee
y(2) = y_ankle(1)
xlim([-100 100]); % we chose the width of the plot, do not fill in coordinates that are outside of the plot size
ylim([-100 100]);
hold on; % hold on so all the plots will be created
plot(x_hip,y_hip,'o'); % plot the hip coordinates as a circle
plot([x_hip x_knee], [y_hip y_knee]) % plotting a line, we connected the hip co with the knee co
% we had to write the x and y co seperatly in a vector
text(x_hip,y_hip,'H') % plotting a text next to the circle of the hip coordinate
plot(x_shoulder,y_shoulder,'o'); % same procedure as explained above
plot([x_shoulder x_hip],[y_shoulder y_hip])
text(x_shoulder,y_shoulder,'S')
plot([x_shoulder x_midhead],[y_shoulder y_midhead])
plot(x_midhead,y_midhead,'o','linewidth',20)
text(x(1),y(1),'K')
text(x(2),y(2),'E')
grid on;
plot(x,y) % plotting the line between the requested knee coordinate (see function)
% and the ankle angle computed above
comet(x,y) % with this function the curve will be followed, so it looks like a drawing
frame = getframe(gcf);
writeVideo(SimulationVraag5Video,frame);
end
end
else % loop for right subjects HR
x_hip = x_knee-30 % we subtract a number from the x-knee coordinate so that the joints will be on the left side of the x-knee coordinate (because right leg tested)
y_hip = y_knee+10
x_shoulder = x_knee-35;
y_shoulder = y_knee+50;
x_midhead = x_knee-35;
y_midhead = y_knee+60;
for k = trialnumber
IKHRdata = importdata(fullfile(pathwayIK_HR(k).folder,pathwayIK_HR(k).name));
DataHRlinks = IKHRdata.data(:,strcmp('knee_angle_r',IKHRdata.colheaders)); % get the knee_angle_r because right leg testings
TimeHRlinks = IKHRdata.data(:,strcmp('time',IKHRdata.colheaders));
kneeangle = IKHRdata.data(:,strcmp('knee_angle_r',IKHRdata.colheaders))
end
for a = 1:100:length(kneeangle)
% computing the lengths of the triangle: femur length (length between knee and hip) and length between hip and ankle
lengthbetweenkneehip = sqrt(((x_knee-x_hip)^2) + (y_knee-y_hip)^2) % computing distance between two coordinates (co hip and co knee are known)
lengthbetweenanklehip = sqrt(tibialength^2 + lengthbetweenkneehip^2 - (2*tibialength * lengthbetweenkneehip * cos(kneeangle(a)))) % law of cosines in a triangle
lengthbetweenanklehipsquared = lengthbetweenanklehip^2 % to get rid of the root square on the other side of the equation
% vgl1 = (xhip-xankle)^2 + (yhip-yankle)^2 - lengthbetweenanklehipsquared
lengthbetweenkneeankle = tibialength % this is known (requested in the funtion as input argument)
lengthbetweenkneeanklesquared = tibialength^2 % to get rid of the root square on the other side of the equation
% vgl2 = (xknee-xankle)^2 + (yknee-yankle)^2 - lengthbetweenkneeanklesquared
syms x_ankle y_ankle
E = [((x_hip-x_ankle)^2 + (y_hip-y_ankle)^2 - lengthbetweenanklehipsquared == 0), ((x_knee-x_ankle)^2 + (y_knee-y_ankle)^2 - lengthbetweenkneeanklesquared == 0)]; % two equations are set to zero, two equations so the two unknown variables of x_ankle and y_ankle will be solved
S = solve(E,x_ankle,y_ankle)
x_ankle = S.x_ankle % there are two possible values for x_ankle
y_ankle = S.y_ankle
x(1) = x_knee
x(2) = x_ankle(1)
y(1) = y_knee
y(2) = y_ankle(1)
xlim([-100 100]);
ylim([-100 100]);
hold on;
plot(x_hip,y_hip,'o');
plot([x_hip x_knee], [y_hip y_knee])
text(x_hip,y_hip,'H')
plot(x_shoulder,y_shoulder,'o');
plot([x_shoulder x_hip],[y_shoulder y_hip])
text(x_shoulder,y_shoulder,'S')
plot([x_shoulder x_midhead],[y_shoulder y_midhead])
plot(x_midhead,y_midhead,'o','linewidth',20)
plot(x,y);
text(x(1),y(1),'K')
text(x(2),y(2),'E')
grid on;
comet(x,y);
frame = getframe(gcf);
writeVideo(SimulationVraag5Video,frame);
end
end
end
end
if condition == 0 % MR trials
for i = TDnumber
dataFolder = [startpath '/TD' num2str(i)];
dataFile = fullfile(dataFolder, '/IK/MR*.mot');
pathwayIK_MR = dir(dataFile);
% for loop for left subjects MR trials
if i == 3 | i == 5 | i == 7 | i == 9 | i == 14
x_hip = x_knee+30
y_hip = y_knee+10
x_shoulder = x_knee+35;
y_shoulder = y_knee+50;
x_midhead = x_knee+35;
y_midhead = y_knee+60;
for k = trialnumber
IKMRdata = importdata(fullfile(pathwayIK_MR(k).folder,pathwayIK_MR(k).name));
DataMRlinks = IKMRdata.data(:,strcmp('knee_angle_l',IKMRdata.colheaders));
TimeMRlinks = IKMRdata.data(:,strcmp('time',IKMRdata.colheaders));
kneeangle = IKMRdata.data(:,strcmp('knee_angle_l',IKMRdata.colheaders));
end
for a = 1:30:length(kneeangle)
% computing the lengths of the triangle: femur length (length between knee and hip) and length between hip and ankle
lengthbetweenkneehip = sqrt(((x_knee-x_hip)^2) + (y_knee-y_hip)^2) % computing distance between two coordinates (co hip and co knee are known)
lengthbetweenanklehip = sqrt(tibialength^2 + lengthbetweenkneehip^2 - (2*tibialength * lengthbetweenkneehip * cos(kneeangle(a)))) % law of cosines in a triangle
lengthbetweenanklehipsquared = lengthbetweenanklehip^2 % to get rid of the root square on the other side of the equation
% vgl1 = (xhip-xankle)^2 + (yhip-yankle)^2 - lengthbetweenanklehipsquared
lengthbetweenkneeankle = tibialength % this is known (requested in the funtion as input argument)
lengthbetweenkneeanklesquared = tibialength^2 % to get rid of the root square on the other side of the equation
% vgl2 = (xknee-xankle)^2 + (yknee-yankle)^2 - lengthbetweenkneeanklesquared
syms x_ankle y_ankle
E = [((x_hip-x_ankle)^2 + (y_hip-y_ankle)^2 - lengthbetweenanklehipsquared == 0), ((x_knee-x_ankle)^2 + (y_knee-y_ankle)^2 - lengthbetweenkneeanklesquared == 0)]; % two equations are set to zero, two equations so the two unknown variables of x_ankle and y_ankle will be solved
S = solve(E,x_ankle,y_ankle)
x_ankle = S.x_ankle % there are two possible values for x_ankle
y_ankle = S.y_ankle
x(1) = x_knee
x(2) = x_ankle(1)
y(1) = y_knee
y(2) = y_ankle(1)
xlim([-100 100]);
ylim([-100 100]);
hold on;
plott = plot(x,y);
grid on;
comet(x,y);
frame = getframe(gcf);
writeVideo(SimulationVraag5Video,frame);
end
else % right subjects MR trials
x_hip = x_knee-30
y_hip = y_knee+10
x_shoulder = x_knee-35;
y_shoulder = y_knee+50;
x_midhead = x_knee-35;
y_midhead = y_knee+60;
for k = trialnumber
IKMRdata = importdata(fullfile(pathwayIK_MR(k).folder,pathwayIK_MR(k).name));
DataMRlinks = IKMRdata.data(:,strcmp('knee_angle_r',IKMRdata.colheaders));
TimeMRlinks = IKMRdata.data(:,strcmp('time',IKMRdata.colheaders));
kneeangle = IKMRdata.data(:,strcmp('knee_angle_r',IKMRdata.colheaders));
end
for a = 1:30:length(kneeangle)
% computing the lengths of the triangle: femur length (length between knee and hip) and length between hip and ankle
lengthbetweenkneehip = sqrt(((x_knee-x_hip)^2) + (y_knee-y_hip)^2) % computing distance between two coordinates (co hip and co knee are known)
lengthbetweenanklehip = sqrt(tibialength^2 + lengthbetweenkneehip^2 - (2*tibialength * lengthbetweenkneehip * cos(kneeangle(a)))) % law of cosines in a triangle
lengthbetweenanklehipsquared = lengthbetweenanklehip^2 % to get rid of the root square on the other side of the equation
% vgl1 = (xhip-xankle)^2 + (yhip-yankle)^2 - lengthbetweenanklehipsquared
lengthbetweenkneeankle = tibialength % this is known (requested in the funtion as input argument)
lengthbetweenkneeanklesquared = tibialength^2 % to get rid of the root square on the other side of the equation
% vgl2 = (xknee-xankle)^2 + (yknee-yankle)^2 - lengthbetweenkneeanklesquared
syms x_ankle y_ankle
E = [((x_hip-x_ankle)^2 + (y_hip-y_ankle)^2 - lengthbetweenanklehipsquared == 0), ((x_knee-x_ankle)^2 + (y_knee-y_ankle)^2 - lengthbetweenkneeanklesquared == 0)]; % two equations are set to zero, two equations so the two unknown variables of x_ankle and y_ankle will be solved
S = solve(E,x_ankle,y_ankle)
x_ankle = S.x_ankle % there are two possible values for x_ankle
y_ankle = S.y_ankle
x(1) = x_knee
x(2) = x_ankle(1)
y(1) = y_knee
y(2) = y_ankle(1)
xlim([-100 100]);
ylim([-100 100]);
hold on;
plott = plot(x,y);
grid on;
comet(x,y);
frame = getframe(gcf);
writeVideo(SimulationVraag5Video,frame);
end
end
end
end
close(SimulationVraag5Video);
this is the code of our other script:
SimulationVraag5Function(3,1,1,30,0,0)
  댓글 수: 1
Lissa
Lissa 2021년 1월 3일
This is the error that we get:
Error using VideoWriter/writeVideo (line 368)
Frame must be 448 by 336
Error in SimulationVraag5Function (line 95)
writeVideo(SimulationVraag5Video,frame);
Error in SimulationVraag5 (line 4)
SimulationVraag5Function(3,1,1,30,0,0)
It does makes a file for the video in the current folder.

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

답변 (1개)

Walter Roberson
Walter Roberson 2021년 1월 5일
https://www.mathworks.com/matlabcentral/answers/329710-how-to-save-a-figure-s-content-as-image#comment_1241428 explains

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by