Print rows of strings above previous csv table

I have looked everywhere for this answer and can't seem to find it. I am attempting to add 4 rows of string text immediately before writing a table to a csv. When I write 4 lines of fprintf functions it adds it to the top of the table but also deletes columns. I have tried dlmwrite but doesn't seem to work. For example, this is what I am trying to do.
text
text
text
text
beginning of table

Answers (1)

I do not know an operating system, which allows to insert bytes at the beginning of a file. Only appending is used.
So your way is to create a new file, print the text and append the contents of the previous file.
% [UNTESTED CODE]
function insertOnTopOfFile(FileName, Text)
tmpFile = tempName;
[status, msg, msgID] = movefile(FileName, tmpFile);
if status == 0
error(msgID, msg);
end
fid = fopen(FileName, 'w');
if fid == -1
error(['YourID:', mfilename, ':FileIO', 'Cannot open file for writing: %s', FileName);
end
fwrite(fid, Text, 'char');
oldText = fileread(tmpFile);
fwrite(fid, oldText, 'char');
fclose(fid);
end

Categories

Asked:

on 25 May 2021

Answered:

Jan
on 25 May 2021

Community Treasure Hunt

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

Start Hunting!