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

Elias Gule
Elias Gule 2018년 3월 29일
please attach your text file, I can help you as soon as now!
Rik
Rik 2018년 3월 29일
What code are you using to import the file? Because the error text is not a usual error message.
Erik Verdijk
Erik Verdijk 2018년 3월 29일
편집: per isakson 2018년 3월 29일
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 2018년 3월 29일
and I get an empty matrix

댓글을 달려면 로그인하십시오.

 채택된 답변

Elias Gule
Elias Gule 2018년 3월 29일

0 개 추천

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

Erik Verdijk
Erik Verdijk 2018년 3월 29일
clear all; close all; answer = 4;
while answer > 1 [FileName,PathName] = uigetfile('../*.txt','MultiSelect','off','title','path'); fid = fopen(fullfile(PathName,FileName)); fileID = fopen(fid); 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','tab'); fclose(fileID); answer = questdlg('Do you want to select an other file?', ... 'Options', ... 'Yes','No','No'); switch answer case 'Yes' answer = 4; case 'No' answer = 1; end while answer > 3 [FileName,PathName] = uigetfile('../*.txt','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','tab'); fclose(fileID); answer = questdlg('Do you want to select an other file?', ... 'Options', ... 'Yes','No','No'); switch answer case 'Yes' answer = 4; case 'No' answer = 1; end end end
Elias Gule
Elias Gule 2018년 3월 29일
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 2018년 3월 29일
Please change your delimiter option from "tab" to "\t".

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

태그

질문:

2018년 3월 29일

댓글:

2018년 3월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by