Trouble with fprintf...
2 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
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 个评论
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
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 个评论
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 Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!