Eghbal
Eghbal

Reputation: 3783

Read a string from text file returning strange characters in MATLAB

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.

Upvotes: 0

Views: 1407

Answers (1)

Yvon
Yvon

Reputation: 2983

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.

Upvotes: 2

Related Questions