How can I append text to a number matrix for export to .txt tab delimited or ASCII file? fprintf is blowing my mind!

2 views (last 30 days)
Hello,
I would like to read in an existing text file, remove a row, then add two header rows to the number matrix and finally export/save/print it to an .txt or ASCII file. So far I have this to read in my numbers from a text file:
%%%%
nCols = 125;
format = repmat('%f', [1 nCols]);
fileID = fopen('07N15_T70_Naive_P7sd.txt');
C = textscan(fileID, format, 'HeaderLines',1,'Delimiter','\t');
fclose(fileID);
for i = 1:124; %124 electrodes
forBESA (i,:) = C{1,i};
end
%%%%%
This works fine and I get my number matrix (.txt file is attached).
What I would like to do then is to add two header lines of text to the number matrix. I made these cell arrays:
%%%%
Head1={'TimePoin','ts=750','Channe','ls=124','BeginSwe','ep[ms]=-','1500','Samplin','gInterva','l[ms]=4.','0','Bins/uV=','1'};
Head2 = {'E1','E2','E3','E4','E5','E6','E7','E8','E9','E10','E11','E12','E13','E14'};
%%%%
Then I tried many combinations of
%%%%
dlmwrite('forBESA.txt', Head1 ,'delimiter', '\t')
dlmwrite('forBESA.txt', Head2 , '-append','delimiter', '\t')
dlmwrite('forBESA.txt', forBESA, '-append','delimiter', '\t')
to absolutely no avail. The format of the headers in the cell array is not preserved.
I have been investigating fprintf and think it could work, but I can't seem to get the formatting right.
Thank you in advance.
Gabriella Musacchia Montclair State U NJ

Accepted Answer

dpb
dpb on 2 May 2014
nCols = 125;
format = repmat('%f', [1 nCols]);
fileID = fopen('07N15_T70_Naive_P7sd.txt');
C = textscan(fileID, format, 'HeaderLines',1,'Delimiter','\t');
fclose(fileID);
for i = 1:124;
%124 electrodes for
BESA (i,:) = C{1,i};
end
Head1='TimePoin','ts=750','Channe','ls=124','BeginSwe', ...
'ep[ms]=-','1500','Samplin','gInterva','l[ms]=4.','0','Bins/uV=','1'};
Head2 = {'E1','E2','E3','E4','E5','E6','E7','E8','E9','E10','E11','E12','E13','E14'};
...
Then I tried many combinations of dlmwrite (...
As the doc's say for dlmwrite it doesn't handle anything but a matrix M as output; no cell arrays or cell strings allowed. Pity, but's that the way it is.
fprintf is it; there's an example of a small table in the Examples in the doc for it.
For your case it's the same idea just more fields...
fmt=[repmat('%10s',1,nCols) '\n']; % the eight chars plus a couple spaces
fprintf(fid,fmt,Head1{:}) % and write them
ditto for second header and then follow up w/ appropriate format string for the array. You may want to adjust the spacing of the shorter to center better in the field widths but that's all just bookkeeping (albeit somewhat tedious).
  2 Comments
Gabriella M
Gabriella M on 2 May 2014
This was SO helpful, dpb. Everything works now. This is the end product:
...
nCols = 125;
format = repmat('%f', [1 nCols]);
fileID = fopen('07N15_T70_Naive_P7sd.txt');
C = textscan(fileID, format, 'HeaderLines',1,'Delimiter','\t');
for i = 2:125; %124 electrodes
forBESA (:,i-1) = C{1,i};
end
Head1 = {'TimePoin',...'Bins/uV=','1'};%...refers to other labels
Head2={'E1','E2','E3','E4','E5','E6','E7','E8','E9','E10','E11','E12','E13','E14',...'E124'}; %...refers to other labels
nCols = 13;
fmt=[repmat('%-10s',1,nCols) '\n']; % the eight chars plus a couple spaces
fid = fopen('forBESA.asc', 'w');
fprintf(fid,fmt,Head1{:}) % and write them
nCols = 124;
fmt=[repmat('%-10s',1,nCols) '\n']; % the eight chars plus a couple spaces
fprintf(fid,fmt,Head2{:}) % and write them
nCols = 124;
fmt=[repmat('%-f ',1,nCols) '\n']; % the numbers in the array
fprintf(fid,fmt,forBESA) % and write them
fclose(fid);
...
Now I just hope my analysis program will be able to read the .asc as space delimited, since I don't see how to make sure that the .asc is saved (printed) in tab delimited format. But I am definitely off to a good start. Too bad I kept butting my head against this problem alone for 3 hours before asking here!
Thanks again and have a good weekend, Gabriella Musacchia
dpb
dpb on 2 May 2014
Edited: dpb on 3 May 2014
nCols = 124;
fmt=[repmat('%-f ',1,nCols) '\n']; % the numbers in the array
fprintf(fid,fmt,forBESA) % and write them
One key point here...Matlab internal storage order is column-major so you needs must transpose the array to write it in "one swell foop".
fprintf(fid,fmt,forBESA.') % and write in row-major order
...
Now I just hope my analysis program will be able to read the .asc as space delimited, since I don't see how to make sure that the .asc is saved (printed) in tab delimited format.
You do that by inserting the tab in the format if you want/need it. However, doing that sorta' interacts and doesn't always play nice with fixed spacing so you may want to remove it when do...
SOTOO...
fmt=[repmat('%-f\t',1,nCols-1) '%-f\n'];
These are all in the doc's from fprintf and friends following the links to the formatting strings. Or, use a C primer as TMW simply vectorized the C i/o functions as you can see from the name conventions.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!