fprintf cell array to .txt file

20 views (last 30 days)
Tyler
Tyler on 11 Jul 2016
Commented: Geoff Hayes on 11 Jul 2016
Hi,
I have a cell Array C that contains vectors of different length. I would like to print this data to a text file in the following format:
C{1} = [65,37,36,12,13,5,64]
C{2} = [189,178,177,176,175,187]
C{3} = [129,1,53,93,95,77,128]
C{4} = [189,187,186,188] (etc)
% C has 100 cells, so I would like to use a loop somehow here
% This is what I would like my output .txt file to look like:
1 (tab) 7 % Where 1 refers to the cell number, 7 refers to the number of elements in the vector
65 % Then each element is listed on its own line
37
36
12
13
5
64
% Skip a line between cells
2 (tab) 6 % Cell number 2, 6 elements in the array
189
178
177
176
175
187
3 (tab) 7
129
1
53
93
95
77
128
4 (tab) 4
189
187
186
188 %Etc
Does anyone know a simple way to do this? I'm thinking I could create 2 separate vectors for the cell number and size of each vector- [1:100] and [7,6,7,4...]. Besides this I'm vaguely aware I will use fprintf and the %d to call C, but besides that I'm not sure. Any help would be much appreciated. Thank you!

Answers (1)

Geoff Hayes
Geoff Hayes on 11 Jul 2016
Tyler - you won't need to create two separate arrays (vectors). To determine the length of an array you would use length. To open, write to, and close a file you would use fopen, fprintf, and fclose respectively. As you have already guessed you will need to use a for loop to iterate over each element in your cell array. You can also use an inner for loop too (for what?).
Your question seems a little too much like homework, so please use the above to get started. When you become stuck, then post your code and the error message (if one exists) so that we can provide some guidance.
  2 Comments
Tyler
Tyler on 11 Jul 2016
Thanks Geoff,
I think I figured it out, I realize now it would have been more helpful to include what I already tried. One of my problems was using \n instead of \r\n to skip a line. for anyone with a similar question, here is what I did. Let me know if you see a flaw in it, for now it looks like it does what I want it to do.
k = [1:100].';
filename = 'newfile.txt';
fid = fopen(filename,'w');
for i = 1:length(C)
fprintf(fid,'\r\n\r\n%d',k(i));
fprintf(fid,'\t%d',size(C{i},2));
for j = 1:size(C{i})
fprintf(fid,'\r\n%d',C{i,j});
end
end
This skips a couple lines at the beginning, but that is an easy manual fix.
Geoff Hayes
Geoff Hayes on 11 Jul 2016
Hi Tyler - I've never had to use the \r as \n works fine for me but I did see the note about this in the link to help for fopen. Since you are creating a text file, you can specify this when you open the file as
fid = open(filename,'wt');
You can also combine your first two fprintf calls as
fprintf(fid,'\r\n\r\n%d\t%d',i,length(C{i}));
Note how the above uses the indexing variable i as is instead of accessing an array k that will return the same value. It is good practice to avoid using i and j as indexing variables as MATLAB uses these to represent the imaginary number.
As for your inner for loop, won't size(C{i}) return an array instead of a scalar? Again, I think that you want to use length (since we are assuming one-dimensional arrays) as
data = C{i};
for v = 1:length(data);
fprintf(fid, '\r\n%d',data(v));
end

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!