Combine writematrix with a text header and safe as file
Show older comments
Hi guys,
I am stuck with this problem since many hours. I want have multible *.IV0 files (with headers). I attached one example AS A *.TXT! It was npt possible to upload *.IV0 files. So please rename it when you try to solve this problem. The fact that it is a *.IV0 file changes many things. Since with this filetype it is not possible to use some matlab codes.
I try to average all files in a specifc folder excluding the header (first 27 rows). This works great so far. Afterwards I wanted to save the averaged data by using writematrix. This works too by using following code:
folder = 'C:\Users\--__aver\';
IV0Files = dir(fullfile(folder, '*.IV0')); % Use absolute path names
numfiles = length(IV0Files);
data_sum = 0;
rows = [1,2,3,4];
for ci = 1:numfiles
data_file = readmatrix(IV0Files(ci).name,"NumHeaderLines",27,"FileType","text");
data_sum = data_sum+data_file(:,rows); % summation accross the files (removed 3rd column (time))
end
% divide by numfiles to have the average (and not the sum)
average = data_sum/numfiles;
averagecell=num2cell(average);
%this is to save the data in the new IV0 file
writematrix(average, fullfile(folder,'otherdirectionSK-ITOLspray16.IV0'),"Delimiter","tab", "FileType", "text");
But now comes the heavy part. I tried to add a header to my output file. The header should be the same as in one of the files I average. So basically the first 27 rows of the attached file. I tried soo much and I can´t figure out a way to do it. Since it is a *.IV0 file things get even more complicated. I have tried things using:
opts (does not work for *.IV0)
fgets (in a for loop (same as code above), got stuck, I think because of the tab delimiter maybe...)
3 Comments
dpb
on 4 Jan 2021
The high-level writeXXX routines don't have the ability to be able to append data to or to be used to append data to existing text files, so that's a non-starter.
Probably the easiest way in coding would be to hold the header lines in a temporary file and then just use the OS copy command to concatenate the two.
There's no reason I can think of why the fgets/fprintf combination wouldn't work; show the code for it as you tried it...note, however, that you can't combine it with writematrix; you'll have to use all low-level i/o functions.
I think the restriction of not having an append option is a real limitation in the general usefulness of the new functions.
Simon Keegan
on 4 Jan 2021
Simon Keegan
on 4 Jan 2021
Accepted Answer
More Answers (1)
Walter Roberson
on 4 Jan 2021
Edited: Walter Roberson
on 23 Aug 2022
0 votes
'writemode', 'append'
2 Comments
dpb
on 4 Jan 2021
Interesting new addition, Walter.
Which release introduced it? R2019b lacks it which is what have here...
Alan Hoskins
on 23 Aug 2022
Yeah, 2019b lacks this functionality. I guess I need to upgrade.
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!