I can't read a .txt using importdata, but it works for .jpg or .xlsx

3 visualizaciones (últimos 30 días)
sasori fan
sasori fan el 15 de Oct. de 2019
Comentada: sasori fan el 17 de Oct. de 2019
I use the MATLAB r2016b for mac. I tried to use the 'importdata' to read a .txt file, even the text is simple enough, I can still get the 'MATLAB has encountered an internal problem and needs to close' warning. I tried to use it on a .jpg or .xlsx, it both works well. Why it refused to work on a simple .txt file like,
# a b c
1 2 3
5 6 7
Is anyone facing the same problem? Is there any good solutions? Thank you very much.

Respuestas (1)

sid Chen
sid Chen el 15 de Oct. de 2019
Sorry, I don`t know the reason.
I am using Maltab 2018b on Win 10, function `importdata(txt_file)` works on my computer.
>> f = importdata('tmp.txt')
f =
包含以下字段的 struct:
data: [2×3 double]
textdata: {'# a b c'}
>> f.data
ans =
1 2 3
5 6 7
>> f.textdata
ans =
1×1 cell 数组
{'# a b c'}
But I usually use another method to read txt file.
FileID = fopen('test.txt','r');
tline = fgetl(FileID);
count = 1;
content = {};
while ischar(tline)
content{count,:} = tline;
tline = fgetl(FileID)
count = count + 1;
end
fclose(FileID);
Also you can try to change from .txt to .csv, and use `csvread` to load data.
  1 comentario
sasori fan
sasori fan el 17 de Oct. de 2019
Thanks for the suggestion. I got the reason from the answer to 'Why does MATLAB crash on macOS Mojave 10.14.4 when I import data or open a file?'. It looks like I need to upgrade to a higher version.

Iniciar sesión para comentar.

Categorías

Más información sobre Low-Level File I/O en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by