Textscan returns empty array

11 vues (au cours des 30 derniers jours)
Balkrishna Patankar
Balkrishna Patankar le 13 Déc 2017
I have difficulty with parsing a file using text scan. The file is as follows:
Statistics;Object ID;Object Class;Area (µm²);Perimeter (µm);Mean (Radius) (µm);Mean (Gray Intensity Value);Mean (Color Intensity Value);Shape Factor;Center of Gravity X (µm);Center of Gravity Y (µm);Equivalent Circle Diameter (µm);Value (Z) (nm)
;1;2;8.77;24.45;0.62;112.42;;0.18;1912.41;313.47;3.34;15271210.00
;2;2;5.54;14.94;0.50;106.92;;0.31;1846.18;25.47;2.66;15271210.00
The code I write is :
[filename,folder]=uigetfile('*.txt');
%Open file
[fileID,errmsg] = fopen(filename);
data_cell = textscan(fileID,'%f%f%f%f%f%f%f%f%f%f%f%f%f', 'delimiter', ';','EmptyValue',-Inf,'HeaderLines',1);
fclose(fileID);
data_cell is always returned empty. Can someone help ?
Thanks
  2 commentaires
Michal Dobai
Michal Dobai le 13 Déc 2017
Modifié(e) : Michal Dobai le 13 Déc 2017
I tested your program and it gives me 1x13 cell array of column vectors representing each column in input text file. So it works for me just fine.
Are you really opening file you mean to open? Because one mistake I can think of, could be opening file outside your current MATLAB folder. If this file has same name as some file in your current MATLAB folder, your code will open file from current folder instead (and you don't have to get any error message).
That's why you should use full path to open your file. Now you're doing this:
fopen(filename);
you should consider to change it to this:
fopen(fullfile(folder,filename));
Balkrishna Patankar
Balkrishna Patankar le 14 Déc 2017
Thanks Michal for your help. The real problem as I figured out was in the encoding.
Changing the fopen to
[fileID,errmsg] = fopen(filename,'r','l','UTF16-LE');
solved the problem for me.

Connectez-vous pour commenter.

Réponse acceptée

Balkrishna Patankar
Balkrishna Patankar le 14 Déc 2017
The problem was with the encoding of the file. Changing the fopen to
[fileID,errmsg] = fopen(filename,'r','l','UTF16-LE');
solved the problem for me.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by