Output my cell array or table to a .txt file and preserve the original row and column orientation

14 Ansichten (letzte 30 Tage)
I have a large amount of data in a cell array (5000x7). I have converted all elements to strings so that I can more easily export the data to a .txt file. I have tried several different things, but can't seem to get my data to the .txt file in the exact same format I currently see it in Matlab.
So for example, if I have a 10x7 CELL array of STRINGS that looks something like this (' ' is an empty string and 's' is just a string): I have separated each string in the cell array with a comma because Mathworks is not outputting this text in the same format that I am originally writing it in (ha ha).
C =
{' ', ' ', ' ', ' ', ' ', ' ', ' ';
' ', ' ', ' ', ' ', ' ', ' ', ' ';
's', 's', 's', ' ', ' ', ' ', ' ';
's', 's', 's', ' ', ' ', ' ', ' ';
's', 's', 's', ' ', ' ', ' ', ' ';
's', 's', 's', 's', 's', 's', 's';
's', 's', 's', 's', 's', 's', 's';
's', 's', 's', 's', 's', 's', 's';
's', 's', 's', 's', 's', 's', 's';
's', 's', 's', 's', 's', 's', 's'}
I want to output this to a .txt file in this EXACT same format--meaning that all of the columns and rows line up correctly.
I have tried:
fileID = fopen('tst.txt','w+');
[nrows,ncols] = size(C);
for row = 1:nrows
fprintf(fileID,'%s\n' ,C{row,:});
end
I have tried a couple other delimiters in exchange for %s\n with no good result. Maybe someone knows exactly what delimiter I should use and this is a simple fix.
I have also tried:
CTable = cell2table(C);
writetable(CTable,'tst.txt','Delimiter', '\t','WriteVariableNames',0);
And in the result the columns are clearly not aligned at all. Even though Mathworks used nearly this exact code on a different table and they were lined up...I am thinking the empty strings are messing it up?
I also tried assigning column variable names to CTable and then outputting it, but had no luck there either. Also I thought I should add that the reason this is such an issue is that this .txt file needs to be read in by another program using textscan.
Any help would be much appreciated! Thanks!

Antworten (1)

Shameer Parmar
Shameer Parmar am 24 Jun. 2016
Hello M275,
for your mentioned C (10x7) string matrix,
your code for writing the data into txt file is correct except line "fprintf(fileID,'%s\n' ,C{row,:});"
if you replace that line with "fprintf(fileID,'%s\n' ,[C{row,:}]);" , it will write the data into txt without any error, but here your row content will be concatenated.
So to put the spaces between the all elements of each row, you need to apply one more 'for loop'.. So the modified code is as follow:
(This is just suggested code, if you want to use..)
fileID = fopen('tst.txt','w+');
[nrows,ncols] = size(C);
for row = 1:nrows
newRow = '';
for i=1:ncols
if (i==ncols)
newRow = [newRow,C{row,i}];
else
newRow = [newRow,C{row,i},' '];
end
end
fprintf(fileID,'%s\n',newRow);
end

Kategorien

Mehr zu Cell Arrays 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!

Translated by