I have a strange behaviour in Matlab dealing with RTF files.
The rtf file is read with this instruction:
cin = textread(filename, '%s', 'delimiter', '\n');
(cin) is a Nx1 cell where N is the number of rows of the file,
so I can edit some specific row.
I write the file RTF with the function below:
function dum= cell2rtf(cin, filename)
[row, col]= size(cin);
fout= fopen(filename, 'w');
for ii=1:row
if(ii<row)
fprintf(fout, '%s\r\n', cin{ii});
else
fprintf(fout, '%s', cin{ii});
end
end
fclose(fout);
The strange behaviour is this one:
If the row cin{x} is a string with content
'19°\cell 19°\cell \cell \cell \cell 70°'
the same row appears like below when the file is written by the function
'19°\cell 19°\cell \cell \cell \cell 70°'
I can't understand why the char '°' becomes '°' in every occurrence
and I'd like to know how this can be corrected.
The problem is that textread() works on plain text files, and RTF files are not plain text files: they are binary-ish files that contain markup and formatting codes, sorta like Word .doc files. textread() is probably encountering those formatting/structural codes and misinterpreting them as plain text characters, and that's where your junk characters like  are coming from.
Could you just save your RTF file as a plain text file and read from that?
Otherwise, you'll need to write an RTF parser or find an RTF parsing library and use that. Matlab works (kind of) easily with Java libraries, so you could use the Apache Tika library's RTFParser or RTFParserKit.
Related
I am trying to output table data to a .dat file where I separate the rows by newlines and the column data by commas. I have this written for the first few rows:
fileID = fopen(strcat(filename,'.dat'), 'wt');
fprintf(fileID, '"","","","","","","",""\n');
fprintf(fileID, '"TIMESTAMP","RECORD","MuxAddress","Averages"\n');
fclose(fileID);
This should generate this text in the file:
"","","","","","","",""
"TIMESTAMP","RECORD","MuxAddress","Averages"
Unfortunately, the code actually generates this text:
"","","","","","","","",
"TIMESTAMP","RECORD","MuxAddress","Averages",
Which you can see has commas at the end of each line. This issue breaks a viewer program that I am using, and I can see no way to fix it. I have not found anyone else saying they have this issue either.
I have done some testing, and if I do a fprintf by itself with a newline, it does not put a comma, but as soon as I put a second fprintf, it creates commas at the end of both lines.
So it turns out that it all came down to file permissions. In the specific case with overwriting the file instead of appending it, the code would add the commas. I never discovered WHY the commas were being added, but I did find that if I appended the file instead of overwriting they went away.
I'm new to matlab programming.I have an image processing code which helps to load a mat file in it. the code accepts .mat file as input with video file in it.
filename=('C:\Users\HP\Desktop\Folder\Image\NVR_ch2_main_cut_35-41.asf');
s=load(filename);
s=struct2cell(s);
M=double(s{1});
if (length(size(M))==4)
M=squeeze(M(:,:,1,:));
end`
Error using load
Unknown text on line number 1 of ASCII file C:\Users\HP\Desktop\Folder\Image\NVR_ch2_main_cut_35-41.asf
"Seh".
Just use v = VideoReader(filename) instead of the load function.
For further information: http://ch.mathworks.com/help/matlab/ref/videoreader.html
Well obviously Matlab won't read your file because it contains things load won't accept.
Does your file comply to this: (from the Matlab reference , next time you should read this)
ASCII files must contain a rectangular table of numbers, with an equal
number of elements in each row. The file delimiter (the character
between elements in each row) can be a blank, comma, semicolon, or tab
character. The file can contain MATLAB comments (lines that begin with
a percent sign, %).
http://de.mathworks.com/help/matlab/ref/load.html#responsive_offcanvas
Read your first sentence. You say you want to load a .mat file. But filename ends with .asf which is some video format if I remember correctly.
You can't feed a video file into load.
I'm reading a string like "1.0.2" from text file with these codes :
reader = fopen('Address\My_Text.txt');
Out= textscan(reader,'%str');
Out1=Out{1} ;
Out2=Out1{1};
fclose(reader);
This code (Out2) returns a string like this: 1.0.2 . This is a text file that copied by MATLAB from other place in HDD and read one time with above code for comparing with some existed text file and after that replace with this file using movefile (The main file is working correctly). When I create a text file manually and insert "1.0.2" in it, These codes read this value correctly. What is the problem? What is the solution for MATLAB?
Thanks.
You can use fopen('My_Text.txt', 'r', 'n', 'UTF-8') to open this file in UTF-8 encoding. For the added 3 parameters, check documentation of fopen for details.
Inserting fseek(reader, 3, 'bof') before textscan may also fix this problem, in a different manner.  is the BOM for UTF-8.
I am using the fprintf command to store the contents of a .mat file to a .txt. The .mat file contains strings. My code prints the data in the same column.
fid = fopen('exp.txt','wt');
for i=1:275
fprintf (fid,classes{i}{1})
end
fclose(fid);
When I use the \n and the '\r\n' options, they doesn't print anything to the file. I'd appreciate the help!
Some text editors will show those new line characters and some wont. This happens because of different standards followed by different softwares and operating systems for eg:
end of line sequences
Windows end of line sequence: \r\n
Unix end of line sequence: \n
Mac end of line sequence: \r
So if you really want good human readable formats, either fix your operation system/software and use characters friendly for that system, or if you want uniformity in the reports, better write files in standard HTML formats :)
adding a "br" tag and naming file .html is as simple as writing out '\n' and naming it .txt!
I am trying to send an array that is [2 x N] doubles large to a text file using the fprintf() command. I am having problems in that fprintf() is not recognizing the new line command (\n) or the carriage return command (\r). The code I am using is
fid = fopen([Image.Dir,'CtlPts_',Image.Files{k},'.txt'],'w');
fprintf(fid,'%.4f\t%.4f\n',control_points{k});
fclose(fid);
where the data I am trying to print is in the cell control_points{k}.
The tab gets printed fine, but everything in the text file gets printed on one line, so this is why I am assuming that it is ignoring my new line character.
Is there something wrong with my syntax that I am not seeing?
I know that on many systems, \n is not enough to create what you're asking for (and so, maybe you have to do \r\n)
An alternative solution is to open the file in text mode, that way MATLAB automatically inserts a carriage return \r before any newline \n character in the output on Windows systems:
fid = fopen('file.txt', 'wt');
fprintf(fid, '%f\t%f\n', rand(10,2));
fclose(fid);
Note that this is somewhat unnecessary, since most editors (with the exception of Microsoft Notepad) recognize Unix/Mac/Windows line endings.