필터 지우기
필터 지우기

Trouble with fprintf...

조회 수: 4 (최근 30일)
Daniel
Daniel 2011년 5월 25일
I need to export data from my GUI to a .txt file. Example of my code below...
[filename, pathname] = uiputfile(efilename,'Save file name');
fullname = fullfile(pathname,filename);
con1e = getappdata(handles.process_condition1,'con1e_app1');
[r1 c1]=size(con1e);
for i=1:c1, fprintf(fullname,'%d,',con1e(1,i)); end
'con1e' contains all the values that I need to export (processed in a separate callback) to a .txt file. Is there a way to use 'con1e' to write my data to the .txt file? It says it can not process cell arrays. There are ~50 values in 'con1e'.... I would like to avoid having to export/import each value in the callback separately if possible. Does uiputfile take the place of fopen? Am I missing fopen and fclose? I'm not getting an error for it. Would sprintf work better? I don't understand the difference between the two... sprintf writes strings and fprintf doesn't? Any help would be greatly appriciated. Thanks!

채택된 답변

Matt Fig
Matt Fig 2011년 5월 25일
You still need FOPEN. UIPUTFILE only gets the name and location for you, which you could use with FOPEN.
fid = fopen(fullname,'wt');
What does the data look like in con1e? You say con1e is a cell array, but what do the cells look like? Are they all the same format of data? Give a short example.
%
%
%
%
%
EDIT Adding an example based on comments and clarifications.
Here is an example that works on my machine:
[filename, pathname] = uiputfile('*.txt','Save file name');
fullname = fullfile(pathname,filename);
fid = fopen(fullname,'wt');
con1e = {'2' 'pizza' '777' '888' '999' 'truth' 'triumph'};
[r1 c1] = size(con1e);
for ii=1:c1,
fprintf(fid,'%s,',con1e{ii});
end
fseek(fid,-1,0);
fprintf(fid,'\n') % Get rid of last comma...
fclose(fid);
  댓글 수: 2
Daniel
Daniel 2011년 5월 26일
So my code should look more like...
[filename, pathname] = uiputfile(efilename,'Save file name');
fullname = fullfile(pathname,filename);
fid = fopen(fullname,'wt');
con1e = getappdata(handles.process_condition1,'con1e_app1');
[r1 c1]=size(con1e);
for i=1:c1, fprintf(fullname,'%s,',con1e(1,i)); end
fclose(fullname)
con1e is 1x50 with either a word(s) or number in each cell... it is made of text and numbers but I have converted everything to strings... thought it would make it easier...
I would like to write con1e to one line in the text file.
Matt Fig
Matt Fig 2011년 5월 26일
See my edit above. I provide you with an example that works on my machine based on your description. You should be able to adapt it as you need.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 5월 25일
fid = fopen('TheFileName.txt', 'wt');
fprintf(fid, '%d, ', [17 9 84]);
fclose(fid)
fprintf formats data and writes it to a file (or to the console).
sprintf formats data and returns the string but does not itself send it to a file (or to the console).
uiputfile() asks the user where they would like the data to go, but does not open the file. You still need the fopen() or way of writing to the file.
Please indicate the values of the following:
size(con1e)
class(con1e)
size(con1e{1})
class(con1e{1})
You did indicate that there are about 50 values in con1e but we need more information about how they are arranged.
Do you want all of the values output on the same line?
  댓글 수: 2
Daniel
Daniel 2011년 5월 26일
So my code should look more like...
[filename, pathname] = uiputfile(efilename,'Save file name');
fullname = fullfile(pathname,filename);
fid = fopen(fullname,'wt');
con1e = getappdata(handles.process_condition1,'con1e_app1');
[r1 c1]=size(con1e);
for i=1:c1, fprintf(fullname,'%s,',con1e(1,i)); end
fclose(fullname)
con1e size is 1x50... con1e class is 'char' (it is mixed words and numbers but I converted them all to strings to make it easier... I hope.
not sure what size(con1e{1}) & class(con1e{1}) are...
I would like to export all the values to one line... however, con1e is the first condition... the user could export up to 8 conditions. The additional conditions need to be on separate lines. I hope that makes sense... let me know if further clarificationwould help. Thanks!
Walter Roberson
Walter Roberson 2011년 5월 26일
[filename, pathname] = uiputfile(efilename,'Save file name');
fullname = fullfile(pathname,filename);
fid = fopen(fullname,'wt');
con1e = getappdata(handles.process_condition1,'con1e_app1');
fprintf(fid,'%s\n', con1e);
fclose(fid);
I suspect, though, that con1e is not char but instead is a cell array in which each element is a char array. If that is the case then:
[filename, pathname] = uiputfile(efilename,'Save file name');
fullname = fullfile(pathname,filename);
fid = fopen(fullname,'wt');
con1e = getappdata(handles.process_condition1,'con1e_app1');
fprintf(fid,'%s\n', con1e{:});
fclose(fid);
Provided that you want the strings one per line. You might want to change the \n to a single space instead, if you want them all on one line with a space between them.

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

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by