read file containing mixed content

Hello,
The format of an input text file is the following:
1 23.08 был
2 367.92 сказал
3 12567.53 когда
...
and so on.
How to read this file so that the following loop can be used:
for i=1:length(file)
str=fileName(i,3);
...
end
Thanks.

9 件のコメント

Stephen23
Stephen23 2022 年 1 月 8 日
b
b 2022 年 1 月 8 日
Hello Stephen,
Already tried that. The third column seems to be a problem.
Here is the readtable output:
Var1 Var2 Var3
____ _____ _____
1 23 {'�'}
2 367 {'�'}
How to read the last column? Thanks.
Rik
Rik 2022 年 1 月 8 日
I personally prefer to do the parsing myself if the automatic one-liner tools fail.
Have you tried reading it as text and doing the parsing with textscan?
b
b 2022 年 1 月 8 日
textscan also gives this output:
ans =
1×1 cell array
{'�'}
for the 3rd column.
Rik
Rik 2022 年 1 月 8 日
Can you attach the file and post your code? That way we can experiment.
Stephen23
Stephen23 2022 年 1 月 8 日
"How to read the last column?"
It works perfectly for me using R2021b (the current online version):
T = readtable('a.txt')
T = 3×3 table
Var1 Var2 Var3 ____ ______ __________ 1 23.08 {'был' } 2 367.92 {'сказал'} 3 12568 {'когда' }
So whatever version or OS you are using is not up-to-date. This also means that the solution will depend on what version (and possibly OS) you are using, which you have not told us.
b
b 2022 年 1 月 9 日
I am genuinely surprised at this output. My OS is windows 10 version 10.0.19044. The matlab version is 2 years old 2020a. With these, the fprintf of the third column is only the question-marks.
Also, on my computer, Var2 appears rounded, and not with the decimal digits as in the output above.
Christopher Creutzig
Christopher Creutzig 2022 年 1 月 11 日
20a may need a hint as to the file encoding you are using. readtable("a.txt","Encoding","UTF-8","NumHeaderLines",0) seems to work for me.
b
b 2022 年 1 月 12 日
Thank you. Thats exactly where the problem was.

サインインしてコメントする。

 採用された回答

Image Analyst
Image Analyst 2022 年 1 月 8 日

0 投票

Try this:
% Get file name
fullFileName = fullfile(pwd, 'a.txt')
if ~isfile(fullFileName)
errorMessage = sprintf('Error: file not found:\n%s', fullFileName);
fprintf('%s\n', errorMessage)
uiwait(errordlg(errorMessage))
return;
end
% Open the file for reading in text mode.
fileID = fopen(fullFileName, 'rt');
% Read the first line of the file.
textLine = fgetl(fileID);
lineCounter = 1;
while ischar(textLine)
% Print out what line we're operating on.
fprintf('%s\n', textLine);
% Split into words.
words = strsplit(textLine);
% Assign to vectors
col1(lineCounter) = str2double(words{1});
col2(lineCounter) = str2double(words{2});
col3{lineCounter} = words{3};
% Read the next line.
textLine = fgetl(fileID);
lineCounter = lineCounter + 1;
end
% All done reading all lines, so close the file.
fclose(fileID);
% Transpose from row vectors into column vectors
col1 = col1'
col2 = col2'
col3 = col3'

3 件のコメント

b
b 2022 年 1 月 9 日
With this, the third column is still not printing properly:
64047 1.11 ������
67221 1.05 ��������
Image Analyst
Image Analyst 2022 年 1 月 9 日
It worked for me with my copy and paste of your original post. If it's not working with your original file, please attach your data file so I can check my program with your actual data file.
b
b 2022 年 1 月 10 日
Hmmm ... oddly and strangely, it is somehow working now ...

サインインしてコメントする。

その他の回答 (0 件)

質問済み:

b
b
2022 年 1 月 8 日

コメント済み:

b
b
2022 年 1 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by