Trouble with fprintf...
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
Matt Fig
am 25 Mai 2011
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 Kommentare
Matt Fig
am 26 Mai 2011
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.
Weitere Antworten (1)
Walter Roberson
am 25 Mai 2011
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 Kommentare
Walter Roberson
am 26 Mai 2011
[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.
Siehe auch
Kategorien
Mehr zu Legend finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!