how could i write cell array containing cell array into a text file using fprintf?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
hi guys , i have used this code to write this cell array into a text-file, but the the text-file i got is adding some abnormal numbers beside the numbers contained in the cell array!!!!
C5 = X;
fid5= fopen('X_file.txt', 'w');
for h=1:217
fprintf(fid5, '%s\t %s\t %s\t %f\t ', C5{h}{:});
fprintf(fid5,'\t');
fprintf(fid5,'\n');
i got like this which is exist here"X-fileoutput" end fclose(fid5);
Accepted Answer
Walter Roberson
on 28 Mar 2017
Your cells all contain character vectors, but you include a %f element, which tells MATLAB to expect a numeric element there. The 45.0 that you see are the %f representation of char(45) which is the '-' that just happens to be present in your character vectors.
11 Comments
chocho
on 28 Mar 2017
Edited: Walter Roberson
on 28 Mar 2017
@Walter Roberson Thanks a lot ,i got it .
fprintf(fid5, '%s\t ', C5{h}{:});
this works well but still i have a little issue when reading the output file in Excel is taking only 16384 columns instead of 17817 columns, however in Notepad all displayed till 17817 ?
Walter Roberson
on 28 Mar 2017
There is nothing you can do about that. Excel has a limit of 16384 columns.
You could write as columns, using a different "sheet" for each variable.
chocho
on 29 Mar 2017
@Walter Roberson yes; because of the limited columns issue in Excel. So I will reverse the file. Thanks for your help and your quick response! I really appreciate it !
chocho
on 29 Mar 2017
@Walter Roberson friend, I want again ask you how could I add a header line which has 17814 columns of string format( vector of 17814*1), means start where those float numbers start as in "X.png" and cell 1 and 2 and 3 will have no header?
i know that i have to use again fprintf (fid5,'%s\t',header{...}.....} and i read also about append but i can't figure out a proper command that can do that?
temp = vertcat(header, X{:}) .';
fmt = [repmat('%s,', 1, length(X)), '%s\n'];
fprintf(fid5, fmt temp{:});
This will put the header as row headers, and each of the 217 entries in X will become columns, each of length 17814.
chocho
on 31 Mar 2017
@Walter Roberson friend, I have tried your code but still getting an error as follow
*Error using vertcat CAT arguments dimensions are not consistent.
*Error in endfile (line 22) temp = vertcat(header, X{:}).'; *
chocho
on 31 Mar 2017
header was a column vector so i turn it to row vector of size 1*17814 and still dimension not consistent!.
this is the code I tried:
fid5= fopen('fileX.txt', 'w');
temp = vertcat(header, X{:}).';
fmt = [repmat('%s,', 1, length(X)), '%s\n'];
fprintf(fid5, fmt, temp{:});
fclose(fid5);
chocho
on 31 Mar 2017
@Walter Roberson hi friend could you see the error i got ?
Walter Roberson
on 31 Mar 2017
Your X is length 17817 per entry but you are trying to put on a header of length 17814 . You need to put in those last 3 header entries.
chocho
on 2 Apr 2017
~@Walter Roberson Great thanks to you Walter Roberson
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)