Function with for loop doesn't always work

조회 수: 1 (최근 30일)
Daniel
Daniel 2024년 4월 21일
댓글: Voss 2024년 4월 22일
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,'//---%s\n', comment);
fprintf(fid,'//---%s\n', datestr(now,0));
fprintf(fid,' char headerTime[] = "%s";\n',datestr(now,0));
fprintf(fid,' int nseg = %d; // number of points\n',nseg);
fprintf(fid,' seg\tmySegs[%d]={ // define the array of points in revs\n',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!
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2024년 4월 21일
How does it "not work"? Please specify in the context of your question.
Is the spacing inconsistent?

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

답변 (2개)

VBBV
VBBV 2024년 4월 21일
With option as rev, it wont work, since the variable deg is undefined
option = 'rev' % with this option it wont
option = '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)
Unrecognized function or variable 'deg'.
  댓글 수: 3
VBBV
VBBV 2024년 4월 22일
편집: VBBV 2024년 4월 22일
If the function is part of bigger script, maybe the fid has changed as result of operations, you can close the open file after every call to function as
lear;
deg = [ones(3,3), zeros(3,1)];
HeaderFileName = "testPath.h";
fid = fopen(HeaderFileName,'W');
comment = "matlab sramp";
if fid == -1
disp('error message')
else
path2header(fid, deg, comment, 'rev')
fclose(fid) % add this line
end
Voss
Voss 2024년 4월 22일
편집: Voss 2024년 4월 22일
deg = deg;

is not necessary.

If option is 'rev', then deg will be as passed in to the function - no need to assign it to itself.

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


Voss
Voss 2024년 4월 22일
fclose the file, and you'll see it's not empty.
  댓글 수: 2
Daniel
Daniel 2024년 4월 22일
Ah yes, that's the fix. I should've noticed this if I wasn't writing the script late at night while extremely tired. Thank you!
Voss
Voss 2024년 4월 22일
You're welcome!

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

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by