I got unread file using Fprintf, and that is the first time I got that error

1 次查看(过去 30 天)
After six days of running, I found the file with the attached case
I know it is a matter of encoding issue, but is there a way that I can read the contents of the file
I really appreciate the help
  3 个评论
Mohamed ElWakil
Mohamed ElWakil 2021-2-4
编辑:Mohamed ElWakil 2021-2-4
The file has strange letters due to mismatched encoding i cannot view with ordinary text editors or even Matlab editor
dpb
dpb 2021-2-4
编辑:dpb 2021-2-4
Looks like mixed formatted and unformatted output...first part of file contains
000 0000 0d 0a 36 78 37 78 34 32 20 04 20 08 20 0d 0a 20 ..6x7x42 ♦ ◘ ..
000 0010 0e 20 19 20 0f 20 10 20 11 20 12 20 16 20 1d 20 ♫ ↓ ☼ ► ◄ ↕ ▬ ↔
000 0020 20 20 14 20 17 20 1a 20 26 20 25 20 28 20 0c 20 ¶ ↨ → & % ( .
000 0030 15 20 1b 20 1c 20 1e 20 22 20 01 20 02 20 07 20 § . ∟ ▲ " ☺ ☻ •
000 0040 03 20 09 20 13 20 05 20 0b 20 06 20 0d 20 23 20 . ‼ ♣ . . #
000 0050 24 20 21 20 1f 20 18 20 2a 20 27 20 29 0d 0a 36 $ ! ▼ ↑ * ' )..6
000 0060 78 37 78 34 32 20 01 20 03 20 08 20 0c 20 04 20 x7x42 ☺ ♥ ◘ . ♦
000 0070 0f 20 0e 20 19 20 1c 20 1e 20 2a 20 22 20 02 20 ☼ ♫ ↓ ∟ ▲ * " ☻
000 0080 05 20 0d 20 10 20 11 20 14 20 0d 0a 20 06 20 0b . ► ◄ ¶ .. .
Spelunking shows...
fid=fopen('LQM_Results.txt','r');
LQM=fread(fid,'*char');
fid=fclose(fid);
ix=find(LQM=='6');
diff(ix(1:10))
ans =
93
93
93
93
93
93
93
93
93
>>
that there are fixed-length records in the file that can be read if knew what the data were. Seeing how written is the key...

请先登录,再进行评论。

回答(1 个)

Animesh Gupta
Animesh Gupta 2022-2-2
Hello ElWakil,
It is my understanding that you are unable to load non-ascii contents of the provided file.
You may refer the following script which demonstrates how to read the non ascii contents, using textscan function in the form of fixed size string vectors. You may also apply strip method to remove whitespaces.
filename = "test.txt";
fid = fopen(filename, 'w');
nbytes = fprintf(fid, '%s\n', "000 0050 24 20 21 20 1f 20 18 20 2a 20 27 20 29 0d 0a 36 $ ! ▼ ↑ * ' )..6");
fclose(fid);
fid=fopen(filename,'r');
LQM=textscan(fid,'%s');
LQM_char = char(LQM{1});
t = strings(1,length(LQM_char));
for i=1:length(LQM_char)
t(1,i) = strip(LQM_char(i,:));
end
disp(t)
I hope it helps.

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by