import an easy file

9 visualizaciones (últimos 30 días)
Erik Verdijk
Erik Verdijk el 29 de Mzo. de 2018
Comentada: Elias Gule el 29 de Mzo. de 2018
I know it is a easy question, but I should be extremely grateful if someone give me and answer. I have a string with a complete path. How do I Import this textfile? I know it is stupid, but i shoud thank you 1000 times if you cam help me. I get the message unable to import file.
  4 comentarios
Erik Verdijk
Erik Verdijk el 29 de Mzo. de 2018
Editada: per isakson el 29 de Mzo. de 2018
clear all;
close all;
[FileName,PathName] = uigetfile('../*.csv','MultiSelect','off','title','path');
fileID = fopen(FileName);
C = textscan(fileID,'%d%d%d%s%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d','HeaderLines',80,'Delimiter','/t');
fclose(fileID);
Erik Verdijk
Erik Verdijk el 29 de Mzo. de 2018
and I get an empty matrix

Iniciar sesión para comentar.

Respuesta aceptada

Elias Gule
Elias Gule el 29 de Mzo. de 2018
In this code:
[FileName,PathName] = uigetfile('../*.csv','MultiSelect','off','title','path');
FileName is simply the name of the file you selected. So if the file is not located in the present working directory, fopen will not be able to read it. So it will return an fid of -1, which is an invalid file 'pointer'.
What you need is to supply the full path of the file to fopen. So doing this:
fid = fopen(fullfile(PathName,FileName));
will return a valid file 'pointer'.
  3 comentarios
Elias Gule
Elias Gule el 29 de Mzo. de 2018
You made a little error.
fid = fopen(fullfile(PathName,FileName)); fileID = fopen(fid);
Is the one that causes the problem.
What you referred to as fileID, is actually what I referred to as fid. So change this line to:
fileID = fopen(fullfile(PathName,FileName));
Elias Gule
Elias Gule el 29 de Mzo. de 2018
Please change your delimiter option from "tab" to "\t".

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by