Trying to print elements of cell array to txt file in a for loop using fprintf
조회 수: 2 (최근 30일)
이전 댓글 표시
Hey everyone, What I am trying to do is read in a series of files and output some data from those files to a series of output files. For each of the input files, the last three letters in the filenames are initials that I need to print in each of the output files. So, for file 1 'audio_1_GMC.TABLE', I pull out the last three letters of this filename
fn = 'audio_1_GMC.TABLE'
initials = fn(end-8:end-6);
the output file should look something like this
Active.FLD
000000000
000000000
------------
MOM(GMC)
%OR
fn = 'audio_1_XYZ.TABLE'
%would be
Active.FLD
000000000
000000000
------------
MOM(XYZ)
and so on...
I know that fprintf does not work for cell inputs, yet I found many many solutions on here to questions just like mine using fprintf. So, I thought it was doable with some sort of 'work around'. Unfortunately, it was not. I'm trying to give enough details, but if there is more info you need, please let me know. Here is my code.
output = dir('*.TABLE');
initials = struct2cell(output)';
for i = 1:length(initials)
tables = initials{i};
initials{i} = {tables(end-8:end-6)};
end
for k = 1:size(output)
file_name = [output(k).name(1:end-10),'.TXT'];
fid = fopen(file_name,'wt');
fprintf(fid,'LIVE TRN ver 2\n');
fprintf(fid,'Active.FLD\n');
fprintf(fid,'000000000\n');
fprintf(fid,'000000000\n');
fprintf(fid,'------------\n');
fprintf(fid,'%s','MOM(');
for ii = 1:size(initials)
fprintf(fid,initials{ii});
end
end
fclose(file_name);
Thanks!
댓글 수: 2
채택된 답변
Guillaume
2015년 2월 2일
I should also note that the initials is a 19X1 cell array where each element is a string
Not according to the code you've posted:
initials{i} = {somestring}
initials would be a cell array where each element is a cell which itself may very well contain a string.
initials(i) = {somestring}
%or
initials{i} = somestring
would be a cell array of strings.
As for printing elements of cell array, fprintf does not accept cell arrays, but you can convert the cell array into a comma separated list and pass that to fprintf:
c = {'aaa', 'bbb', 'cdef', 'ghijkl'}
fprintf('%s, ', c{:});
fprintf('\n');
댓글 수: 2
Guillaume
2015년 2월 4일
As per my answer the main problem with your code is the line
initials{i} = {somestring}
This creates a cell array of cell array of string, the solution is in my post above.
There are more issues:
- One bug waiting to bite you given the right conditions is the use of length. length(initials) will be the number of files if there are 5 or more of these files, but always 5 if there are less. That's because length is the size of the largest dimension, and if there are less than 5 rows (files), it's then the number of columns (always 5, the number of fields of the structure). Always be explicit on the dimension whose size you want:
for i = 1:size(initials, 1) %always want the number of rows
- In any case, I don't see the point of transforming the structure returned by dir into a cell array, just to extract the filenames. The following works just as well and is less confusing:
output = dir('*.TABLE');
for i = 1:numel(output)
initials{i} = output(i).name(end-8:end-6);
end
- Your second loop starts with
for k = 1:size(output)
You're lucky that works. I actually had to go and look up the behaviour of colon when passed vectors. If you have ten file, what you have written is equivalent to:
for k = 1:[10 1]
since size always return at least two values. You're better off with:
for k = 1:numel(output) %since it's a vector
- Then in your second loop, you've got an inner loop going over all the values of initials (never mind that the syntax of size is again unexpected), and printing them all for each file. From your description, it doesn't sound like what you want, so get rid of this inner loop, it's just:
fprintf(fid, initials{k});
And in that case, you could also not bother with creating the initials cell array, so no first loop, and just do:
fprintf(fid, output(i).name(end-8:end-6));
Which I would actually collapse with the previous line into:
fprintf(fid 'MOM(%s)\n', output(i).name(end-8:end-6));
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!