Combine writematrix with a text header and safe as file
57 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Mathieu NOE
2021-1-4
hello simon
welcome back and happy new year !
I somehow recognize this code.... and now comes the upgraded version that appends the data to the headers
try this ( after adjusting the folder path) :
folder = cd;
% 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); % not needed as we use writematrix
% retrieve headers line (from first file)
lines= readlines(IV0Files(1).name);
header_lines = convertStringsToChars(lines(1:27,:));
%this is to save the data in the new IV0 file
writecell(header_lines, fullfile([folder '\out'],'otherdirectionSK-ITOLspray16.IV0'),"Delimiter","tab", "FileType", "text");
writematrix(average, fullfile([folder '\out'],'otherdirectionSK-ITOLspray16.IV0'),"Delimiter","tab", "FileType", "text","WriteMode","append");
9 个评论
Mathieu NOE
2021-1-4
you're welcome !!
pretty happy to start the new year on a good tempo ! at least that worked today !!
更多回答(1 个)
Walter Roberson
2021-1-4
编辑:Walter Roberson
2022-8-23
'writemode', 'append'
2 个评论
dpb
2021-1-4
Interesting new addition, Walter.
Which release introduced it? R2019b lacks it which is what have here...
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!