how could i write cell array containing cell array into a text file using fprintf?

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);

 采纳的回答

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 个评论

@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 ?
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.
@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 !
@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.
@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{:}).'; *
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);
@Walter Roberson hi friend could you see the error i got ?
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.
~@Walter Roberson Great thanks to you Walter Roberson

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by