How to read any file by using txt ?

I have a file which is different than txt file but I would like to read it as txt file. It is like right clicking to the icon and choosing 'open with txt' section. I would like to do in matlab and change some parameters inside the txt file. Thanks

Answers (1)

The only difference between reading a binary file versus a text file is to translate end-of-line characters. Other than that (and implicitly, the types of characters you get in text versus binary), there's no difference. I would recommend you read the file as binary.
Unless you're saying that it is ACTUALLY a text file but just has a different file extension. Then I would recommend opening in text mode as per usual.
To open in binary mode:
fid = fopen( 'myfile.dat', 'rb' );
allbytes = fread(fid); % this call reads all characters from the file
fclose(fid);
To open in text mode:
fid = fopen('myfile.dat', 'rt' );
line = fgetl(fid); % this call reads a single line from the file
fclose(fid);
Have a look at this:
[EDIT] From your example...
infile = fopen( 'myfile.$PJ', 'rt' );
outfile = fopen( 'myfile_processed.$PJ', 'wt' );
while ~feof(infile)
line = fgetl(infile);
toks = textscan(line, '%s%f', 1);
if ~isempty(toks)
switch toks{1}
case {'HEIGHT', 'HOFFST', 'TOWHT'}
line = sprintf( '%s %d', toks{1}, fix(toks{2}+1) );
end
end
fprintf( outfile, '%s\n', line );
end
fclose(infile);
fclose(outfile);

6 Comments

Actually the file that I would like to open as txt is not the a text. The extention of the file is .$PJ So, I used the following lines to read the file;
fid = fopen( 'C:\Documents and Settings\guestaskin\Desktop\model\idling.$PJ', 'rb' );
allbytes = fread(fid); % this call reads all characters from the file
fclose(fid);
But I only get the array consisting of numbers when I read the file.I would like to read all the written information inside the file such as numbers, letters, space or characters like ' , " \ . Then I would like to change some numbers inside the file corresponding to some keywords.
For Exp: I have three lines like that
HEIGHT 88.18
HOFFST 0
TOWHT 86.35
I would like to change like ;
HEIGHT 89
HOFFST 1
TOWHT 87
Okay, so as I mentioned, the file might BE a text file but not have the 'txt' file extension. I can create a text file with the extension ".EXE" if I want. It wouldn't change the way the data is stored, and you should still open it as text. I will edit my answer with some example for you to try.
If the file might be UTF encoded and you want the multi-byte encoded characters to be decoded into 32 bit characters, then:
fread(fid, '*char')
If however you do *not* want any UTF encoding,
fread(fid, 'char*1=>char')
I would like to thank you Geoff for the explanations on .txt and other file extentions. But I still have some problems. For ex, I have some empty lines inside the folder and after these empty lines, there is always line with the data. So the 'if ~isempty(toks)' conditional command does not check the entire file for the key words. Secondly, there is a problem in the 'switch toks{1}' . Matlab gives an error like this : SWITCH expression must be a scalar or string constant. I guess it is because of some lines in the file. There are some lines with only numbers such as
NFOIL 6
FTYPE F
NTHICK 2
NREYN 3
6
-1
-1
5
-1
-1
NFOIL 7
I am not interested in those lines because I dont need to change them. But at least I have to keep these lines on the newly created file for other calculations.
Maybe the infile and outfile should be the same file. Then I can skip reading and writing of the irrelevant to another outfile and I can only make some changes inside the original file.
Oh, I was out by one level of indirection =( The tokens are returned as a cell array, and the string in the first cell is itself a cell... So you need something like:
if ~isempty(toks) && iscell(toks{1})
Then you need to turn all the toks{1} references into toks{1}{1}
You could've worked this out by reading that error message and using the debugger! =P Don't give up and think of crazy alternative solutions just because there is an error in the code. I wrote that code into a web browser as a rough guide for you to follow.
As for reading and writing the same file, I wouldn't recommend it... But yes, you can, and in your case you would have to make sure you are replacing exactly the same number of bytes which may involve replacing some with spaces... The problem comes when you want to insert a number that is longer, and you need to shuffle all the rest of the bytes in the file.... So please don't go and make your problem more complicated than it needs to be. If you don't follow this piece of advice, then run into trouble and ask me to help, you'll be disappointed! =)

Sign in to comment.

Categories

Asked:

on 22 May 2012

Community Treasure Hunt

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

Start Hunting!