How to read this file in MATLAB?

I want to access the data in a file. The file is a TextEdit Document that appears to be unsupported. How do I do this? When I input fileID = fopen(filename,'r'), the command returns 3. I guess I will need to convert the file and then read it, but how?

6 Commenti

"The file is a TextEdit Document."
Please upload the file by clicking the paperclip button.
L'O.G.
L'O.G. il 28 Mar 2022
Modificato: L'O.G. il 28 Mar 2022
@Stephen When I try to do so, I get an error message saying that the file format is unsupported. What do you suggest? Is there a way to convert it if necessary?
"What do you suggest?"
Append '.txt' to the file extension and then upload it. Do not remove the existing extesion or change the file in any way.
L'O.G.
L'O.G. il 28 Mar 2022
Thanks. Here you go. Is there a way to automatically (i.e., using MATLAB) append '.txt' to the file extension?
The readtable function is able to read it. There are two non-empty entries in ‘Var8’ and ‘Var9’ that may be non-printable characters.
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/943824/test.trj.txt', 'VariableNamingRule','preserve')
T1 = 54809×9 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 ____ ____ ____ ____ ____ ____ ____ __________ __________ 0 1 0 36 78 96 -1 {0×0 char} {0×0 char} 1 2 0 37 79 95 -1 {0×0 char} {0×0 char} 2 0 0 36 78 94 -1 {0×0 char} {0×0 char} 3 0 0 35 79 93 -1 {0×0 char} {0×0 char} 4 2 0 36 79 93 -1 {0×0 char} {0×0 char} 5 0 0 36 80 92 -1 {0×0 char} {0×0 char} 6 2 0 36 81 91 -1 {0×0 char} {0×0 char} 7 2 0 36 82 90 -1 {0×0 char} {0×0 char} 8 2 0 37 82 89 -1 {0×0 char} {0×0 char} 9 5 0 37 83 88 -1 {0×0 char} {0×0 char} 10 8 0 38 84 87 -1 {0×0 char} {0×0 char} 11 1 0 38 85 86 -1 {0×0 char} {0×0 char} 12 8 0 38 86 86 -1 {0×0 char} {0×0 char} 13 2 0 39 85 86 -1 {0×0 char} {0×0 char} 14 1 0 40 86 85 -1 {0×0 char} {0×0 char} 15 2 0 40 86 86 -1 {0×0 char} {0×0 char}
Vars_8_9_Info = [nnz(~isempty(T1.Var8)); nnz(~isempty(T1.Var9))]
Vars_8_9_Info = 2×1
1 1
Var_8_Content = cellfun(@double,T1.Var8(~isempty(T1.Var8),:), 'Unif',0);
Var_9_Content = cellfun(@double,T1.Var9(~isempty(T1.Var9),:), 'Unif',0);
.
hi
if you simply want a numerical array you can use also readmatrix
out = readmatrix('test.trj.txt' ,"NumHeaderLines", 9)

Accedi per commentare.

Risposte (2)

Stephen23
Stephen23 il 29 Mar 2022
Modificato: Stephen23 il 29 Mar 2022
"I guess I will need to convert the file and then read it, but how?"
Why do you "guess" that? READTABLE has no problem importing the file data:
copyfile('test.trj.txt','test.trj') % just for this forum
fnm = 'test.trj';
opt = detectImportOptions(fnm, 'filetype','delimitedtext', 'NumHeaderLines',9, 'ExpectedNumVariables',7);
tbl = readtable(fnm,opt);
tbl.Properties.VariableNames = {'id','type','mol','x','y','z','bP'}
tbl = 54809×7 table
id type mol x y z bP __ ____ ___ __ __ __ __ 0 1 0 36 78 96 -1 1 2 0 37 79 95 -1 2 0 0 36 78 94 -1 3 0 0 35 79 93 -1 4 2 0 36 79 93 -1 5 0 0 36 80 92 -1 6 2 0 36 81 91 -1 7 2 0 36 82 90 -1 8 2 0 37 82 89 -1 9 5 0 37 83 88 -1 10 8 0 38 84 87 -1 11 1 0 38 85 86 -1 12 8 0 38 86 86 -1 13 2 0 39 85 86 -1 14 1 0 40 86 85 -1 15 2 0 40 86 86 -1

2 Commenti

copyfile isn't ideal because the files are huge, and readtable generates an error (ok on R2021b, but the error shows up on R2018a as having to do with readData)
Stephen23
Stephen23 il 2 Apr 2022
Modificato: Stephen23 il 2 Apr 2022
"copyfile isn't ideal because the files are huge"
COPYFILE is, exactly as I wrote in my answer, just for this forum. Of course you do NOT need to use COPYFILE.
I just used it to get around the restricted file extensions that this forum supports. Another approach would have been to zip it up and then use UNZIP... but would you need to do that with your file? NO. Of course you use your file.

Accedi per commentare.

Prodotti

Release

R2021b

Richiesto:

il 28 Mar 2022

Modificato:

il 2 Apr 2022

Community Treasure Hunt

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

Start Hunting!

Translated by