Trouble with fprintf...

2 Ansichten (letzte 30 Tage)
Daniel
Daniel am 25 Mai 2011
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!

Akzeptierte Antwort

Matt Fig
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
Daniel
Daniel am 26 Mai 2011
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 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
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
Daniel
Daniel am 26 Mai 2011
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 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.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by