Reputation: 27
I am having a trouble with fprintf
and I would so much appreaciate if you could help me:
In the programme on which I am working now, I am tracking the mouse position when the mouse is clicked using get(gca, ‘currentpoint’)
and then I save the final position using fprintf
. Everything works smoothly when I do not define a particular axis position in the figure window; however when I use set(gca, ‘visible’, ‘off’, ‘position’, [])
command to define a particular axis position, fprintf
sometimes writes down a string, either K
or á
, on the text file, which then gives the obvious error for dlmread
when the file is tried to be read. I wonder what might be the cause of those strings on the text file.
Here's the code:
mouse = get(gca, 'currentpoint');
A = mouse(1, 1);
B = mouse(1, 2);
save x_center4.txt A -ascii;
save y_center4.txt B -ascii;
A = load('C:\MATLAB6p5\work\x_center4.txt');
B = load('C:\MATLAB6p5\work\y_center4.txt');
fid = fopen('grand_xcenter4.txt', 'a');
fid2 = fopen('grand_ycenter4.txt', 'a');
fprintf(fid, '%s %d\n', A);
fprintf(fid2, '%s %d\n', B);
fclose(fid);
fclose(fid2);
Upvotes: 0
Views: 1514
Reputation: 12693
fprintf(fid, '%s %d\n', A);
fprintf(fid2, '%s %d\n', B);
You are providing only 1 argument after the format string, when the format string specifies two arguments: a string (%s
) and a base 10 signed integer (%d
)?
This is most likely the cause of the strange characters you are reporting. It may help to know what the type and value of A
and B
are before the fprintf
calls.
I've been looking for official documentation of what constitutes "Undefined Behavior" in MATLAB, but have yet to find a good source. However, failing to provide the right number and type of arguments that the string specifies almost certainly qualifies.
Upvotes: 2