How to clear/delete contents of .txt file in matlab - matlab

How can I either delete the contents of a .txt file so that it becomes a blank file or it deletes the file altogether? I'm reading in from the .txt file and if I don't delete the contents from the previous time it ran, it will omit results that I want to account for.

Suppose your file is named to_be_deleted.txt, you can simply use the following command to delete the file altogether:
delete 'to_be_deleted.txt';
On the other hand, if you simply want to clear it's contents, just open it using fopen with the write attribute as follows:
fopen('to_be_deleted.txt','w');

Related

Powershell Only:File name change according to mapping file and then copy to another directory

I have a mapping file in import.CSV as following:
Name|Business
Jack|Carpenter
Rose|Secretary
William|Clerk
Now, I have a directory which contains files like
90986883#Jack#Sal#1000.dat
76889992#Rose#Sal#2900.dat
67899279#William#Sal#1900.dat
12793298#Harry#Sal#2500.dat
Please note #Sal will always be there after Name. I need to pick these files and put into another directory and end result in second directory should look like.
90986883#Carpenter#Sal#1000.dat
76889992#Secretary#Sal#2900.dat
67899279#Clerk#Sal#1900.dat
Basically Files need to be renamed based upon CSV file and if Name is not there in file name , then there is no action required. Please note that source file should not be changed.
I will appreciate all kind of help.

Combine data from multiple .log files and reading them without creating new .txt file in MATLAB

At the moment I have working code that can read multiple .log files and create a giant .txt file which contains all their data. Then I simply read the giant .txt file. However, this code is inefficient because it involves writing a new .txt file every time I want to run the script.
Does someone know how to simplify this so that it doesn't write a new .txt file every time? Ideally by creating a 'virtual' .txt file?
files=dir('IndividualFiles.*.log');
fileout='GIANT.txt';
fout=fopen(fileout,'w');
for cntfiles=1:length(files)
fin=fopen(files(cntfiles).name);
temp = fread(fin,'uint8');
fwrite(fout,temp,'uint8');
fprintf(fout,'\n');
fclose(fin);
end
fclose(fout);
filename = 'C:\Users\myname\Desktop\Learning Documents\myfolder\GIANT.txt';

random file access in C --- shrink file?

easy question.
I want to create a set of records (database is an overstatement) on disk. I can open with rb+, move to a random location with fseek, and then fread and fwrite, all interspersed with fflush. all good.
now I want to delete one record. easy---move the last record to the spot where I want to delete another record, and then shorten the file by one record.
but...how do I shorten my existing file?
/iaw
Copy the contents before and after the record to be deleted to
a temporary file.
Delete the original file (which had the record to be deleted).
Rename temporary file
as the original file.
To truncate the existing file that you have opened, seek to the desired file size and then set a new EOF at that position. The problem is that C has no function for that purpose. You would have to use platform-specific APIs, like SetEndOfFile() on Windows.
there is no ANSI solution to this problem, short of copying the whole file anew.
there are OS-specific solutions only. see comments under the answers.
Windows: SetEndOfFile()
Posix: ftruncate() and truncate()

Save filename.m deleted my origonal filename.m file and replaced it with a new file. How to get the old back?

I tried to save my file using the command window in Matlab. Unfortunately it replaced my file with a new file. And now I am unable to get it back.
It's probably easy, but I am new to using the command window in Matlab.
You are out of luck on this one. Saving to a file will irretrievably overwrite any existing file with that name unless you specify otherwise with the -append option. In the future, if you have a data set that is important because it is either not reproducable or because it takes a long time to generate it, I would recommend either backing it up or saving it with a timestamp. This is one example:
function save_t(name,varargin)
save(sprintf('%s-%d',name,time),clock*[1e8 1e6 1e4 1e2 1 0].',varargin{:});
end
Save this to a file in you matlab path named "save_t.m" and then you can simply call it just like you would call the save function, but now it will add on a timestamp.
save_t filename
This will help to ensure that you don't accidentally overwrite an existing file.

MATLAB fopen to open a file which doesn't have a file extension

I have a file called lol on C:\. So when I run fopen('C:\lol','a'); to open the file, MATLAB can't open it. I think it's looking for a .txt or .m file extension.
How do I open this file in append mode as I want to append data to the file?
You probably don't have UAC (user access control) to create/modify files in C:\. Try some "safe" location like your Desktop.
Try turning on file extensions. Why can't you just change the format of the file to a .txt or whatever format you want.