Function with for loop doesn’t always work
I’ve written a function that writes a C structure into a header file using fprintf(). It seems to not work at all when the input array is small and works partially when the input array is big.
function deg = path2header(fid, deg, comment, option)
% Print path into a header file
% The angle can be set as ‘rad’, ‘deg’, or ‘rev’ in the option.
% fid – File indentity
% deg – [deg, vmax, amax, dwell]
% comment – Name of the path
% option – ‘rad’, ‘deg’, or ‘rev’
if strcmp(option,’rad’)
deg = deg./(2*pi);
elseif strcmp(option,’deg’)
deg = deg./360;
elseif strcmp(option,’rev’)
else
disp("Error option in path2header()")
return
end
[nseg,m]=size(deg);
disp(nseg)
fprintf(fid,’//—%sn’, comment);
fprintf(fid,’//—%sn’, datestr(now,0));
fprintf(fid,’ char headerTime[] = "%s";n’,datestr(now,0));
fprintf(fid,’ int nseg = %d; // number of pointsn’,nseg);
fprintf(fid,’ segtmySegs[%d]={ // define the array of points in revsn’,nseg);
for i = 1:nseg-1
fprintf(fid,’ {‘);
for j = [1,2,3]
fprintf(fid,’%e, ‘,deg(i,j));
end
fprintf(fid,’%e},n’,deg(i,4));
end
fprintf(fid,’ {‘);
for j = [1,2,3]
fprintf(fid,’%e, ‘,deg(nseg,j));
end
fprintf(fid,’%e},n’,deg(nseg,4));
fprintf(fid,’}n };n’);
This is an example of the test code that results in an empty testPath.h file.
clear;
deg = [ones(3,3), zeros(3,1)];
HeaderFileName = "testPath.h";
fid = fopen(HeaderFileName,’W’);
comment = "matlab sramp";
path2header(fid, deg, comment, ‘rev’)
The function is part of a bigger script to convert angles from an inverse kinematics equation to be saved as C header file to preprogram a motor’s path thus the n x 4 matrix.
Please help. Thx!I’ve written a function that writes a C structure into a header file using fprintf(). It seems to not work at all when the input array is small and works partially when the input array is big.
function deg = path2header(fid, deg, comment, option)
% Print path into a header file
% The angle can be set as ‘rad’, ‘deg’, or ‘rev’ in the option.
% fid – File indentity
% deg – [deg, vmax, amax, dwell]
% comment – Name of the path
% option – ‘rad’, ‘deg’, or ‘rev’
if strcmp(option,’rad’)
deg = deg./(2*pi);
elseif strcmp(option,’deg’)
deg = deg./360;
elseif strcmp(option,’rev’)
else
disp("Error option in path2header()")
return
end
[nseg,m]=size(deg);
disp(nseg)
fprintf(fid,’//—%sn’, comment);
fprintf(fid,’//—%sn’, datestr(now,0));
fprintf(fid,’ char headerTime[] = "%s";n’,datestr(now,0));
fprintf(fid,’ int nseg = %d; // number of pointsn’,nseg);
fprintf(fid,’ segtmySegs[%d]={ // define the array of points in revsn’,nseg);
for i = 1:nseg-1
fprintf(fid,’ {‘);
for j = [1,2,3]
fprintf(fid,’%e, ‘,deg(i,j));
end
fprintf(fid,’%e},n’,deg(i,4));
end
fprintf(fid,’ {‘);
for j = [1,2,3]
fprintf(fid,’%e, ‘,deg(nseg,j));
end
fprintf(fid,’%e},n’,deg(nseg,4));
fprintf(fid,’}n };n’);
This is an example of the test code that results in an empty testPath.h file.
clear;
deg = [ones(3,3), zeros(3,1)];
HeaderFileName = "testPath.h";
fid = fopen(HeaderFileName,’W’);
comment = "matlab sramp";
path2header(fid, deg, comment, ‘rev’)
The function is part of a bigger script to convert angles from an inverse kinematics equation to be saved as C header file to preprogram a motor’s path thus the n x 4 matrix.
Please help. Thx! I’ve written a function that writes a C structure into a header file using fprintf(). It seems to not work at all when the input array is small and works partially when the input array is big.
function deg = path2header(fid, deg, comment, option)
% Print path into a header file
% The angle can be set as ‘rad’, ‘deg’, or ‘rev’ in the option.
% fid – File indentity
% deg – [deg, vmax, amax, dwell]
% comment – Name of the path
% option – ‘rad’, ‘deg’, or ‘rev’
if strcmp(option,’rad’)
deg = deg./(2*pi);
elseif strcmp(option,’deg’)
deg = deg./360;
elseif strcmp(option,’rev’)
else
disp("Error option in path2header()")
return
end
[nseg,m]=size(deg);
disp(nseg)
fprintf(fid,’//—%sn’, comment);
fprintf(fid,’//—%sn’, datestr(now,0));
fprintf(fid,’ char headerTime[] = "%s";n’,datestr(now,0));
fprintf(fid,’ int nseg = %d; // number of pointsn’,nseg);
fprintf(fid,’ segtmySegs[%d]={ // define the array of points in revsn’,nseg);
for i = 1:nseg-1
fprintf(fid,’ {‘);
for j = [1,2,3]
fprintf(fid,’%e, ‘,deg(i,j));
end
fprintf(fid,’%e},n’,deg(i,4));
end
fprintf(fid,’ {‘);
for j = [1,2,3]
fprintf(fid,’%e, ‘,deg(nseg,j));
end
fprintf(fid,’%e},n’,deg(nseg,4));
fprintf(fid,’}n };n’);
This is an example of the test code that results in an empty testPath.h file.
clear;
deg = [ones(3,3), zeros(3,1)];
HeaderFileName = "testPath.h";
fid = fopen(HeaderFileName,’W’);
comment = "matlab sramp";
path2header(fid, deg, comment, ‘rev’)
The function is part of a bigger script to convert angles from an inverse kinematics equation to be saved as C header file to preprogram a motor’s path thus the n x 4 matrix.
Please help. Thx! for loop, function MATLAB Answers — New Questions