Problem with fprintf

Hi All,
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 with the dlmread when the file is tried to be read. I wonder what might be the cause of those strings on the text file.
Thank you very much,
inci

댓글 수: 4

Oleg Komarov
Oleg Komarov 2012년 5월 28일
Post an example that generates the issue.
inci
inci 2012년 5월 28일
Thanks but my code is pretty long and complicated (and needs some jpeg files already saved in the directory to run) and everything's dependent on everything so I do not know how can I simplify to put it here for a quick look. I would so much appreaciate some general comments on why fprintf might be writing random strings on the number file, though, if possible?
Oleg Komarov
Oleg Komarov 2012년 5월 28일
We don't even know the syntax you're using for fprintf!
Summarizing a problem is part of being a good programmer. Not being able to do so, means you don't have all the relevant aspects under control. It's not a critic, it's a state that we've all been through. You can try to deal it by yourself or you can ask for help (and I would always encourage the latter) but it should be done properly.
A wild guess:
>> fprintf('%s\n','224')
224
>> fprintf('%s\n',224)
à
inci
inci 2012년 5월 28일
Thanks... My syntax is:
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);
I guess your suggestion could well be the reason. I am trying to fix my code.

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

답변 (1개)

Oleg Komarov
Oleg Komarov 2012년 5월 28일

0 개 추천

Transpose A and B:
fprintf(fid,'%s %d\n',A');
fprintf(fid,'%s %d\n',B');
As I understand, you saved two columns, respectively of strings and of numbers. However, fprintf() will process A and B along the first dimension (rows) and will switch to the second one only once the first is over.
To be more clear, I think you expect that frpintf will take a string, than a number, then will go to row 2 and repeat the same, but the default behaviour is to go down till the end THEN switch to the second column, thus you need to transpose.

댓글 수: 3

inci
inci 2012년 5월 28일
I do not save A and B on the same text file, actually. I open 2 different text files (fid, and fid2) to save mouse position x coordinate and mouse position y coordinate. Both of them are the number files and save double array numbers. But in fact, as you suggested, when I have multiple rows, somehow it creates the problem. First, I read mouse position and save. Then, load it and append it to the fid and fid2 files one after the other on each iteration. I suspect there might be a problem with my fprintf syntax? Many thanks for your time!
inci
inci 2012년 5월 28일
Shall I convert the double arrays (obtained from the load command) to cell before feeding them to fprint?
Oleg Komarov
Oleg Komarov 2012년 5월 28일
fprintf() doesn't accept cell arrays.
As I say above, transpose your A (whatever you called) when it has multiple rows.

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

카테고리

도움말 센터File Exchange에서 Text Data Preparation에 대해 자세히 알아보기

태그

질문:

2012년 5월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by