Keep getting an error with my function regarding textscan()

I'm trying to import a text file as a 2 row vector and keep getting this back,
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in importfile (line 36)
textscan(fileID, '%[^\n\r]', startRow(1)-1, 'WhiteSpace', '', 'ReturnOnError', false);

Answers (2)

When you used fopen(), it probably returned an invalid file handle, like the file doesn't exist, is locked, or is readonly. Did you check the value of fileID that fopen returned? You forgot to show us the fopen() line of code.

4 Comments

when we imported the data from the import data we created a function with it, and this was my code:
[t,v]=importfile('LA.txt')
%IMPORTFILE Import numeric data from a text file as column vectors.
% [DATETIME2142017602PM,VARNAME2] = IMPORTFILE(FILENAME) Reads data from
% text file FILENAME for the default selection.
%
% [DATETIME2142017602PM,VARNAME2] = IMPORTFILE(FILENAME, STARTROW,
% ENDROW) Reads data from rows STARTROW through ENDROW of text file
% FILENAME.
%
% Example:
% [datetime2142017602PM,VarName2] = importfile('LA',4, 10003);
%
% See also TEXTSCAN.
% Auto-generated by MATLAB on 2017/02/18 15:59:04
%%Initialize variables.
delimiter = '\t';
if nargin<=2
startRow = 4;
endRow = inf;
end
%%Format for each line of text:
% column1: double (%f)
% column2: double (%f)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%f%f%[^\n\r]';
%%Open the text file.
fileID = fopen(filename,'r');
%%Read columns of data according to the format.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
textscan(fileID, '%[^\n\r]', startRow(1)-1, 'WhiteSpace', '', 'ReturnOnError', false);
dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'ReturnOnError', false, 'EndOfLine', '\r\n');
for block=2:length(startRow)
frewind(fileID);
textscan(fileID, '%[^\n\r]', startRow(block)-1, 'WhiteSpace', '', 'ReturnOnError', false);
dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'ReturnOnError', false, 'EndOfLine', '\r\n');
for col=1:length(dataArray)
dataArray{col} = [dataArray{col};dataArrayBlock{col}];
end
end
%%Close the text file.
fclose(fileID);
%%Post processing for unimportable data.
% No unimportable data rules were applied during the import, so no post
% processing code is included. To generate code which works for
% unimportable data, select unimportable cells in a file and regenerate the
% script.
%%Allocate imported array to column variable names
datetime2142017602PM = dataArray{:, 1};
VarName2 = dataArray{:, 2};
OK, but you still have not answered my question "Did you check the value of fileID that fopen returned?" Please answer. What is it?
Also, where is importfile defined? You call it twice, here:
[t,v]=importfile('LA.txt')
%IMPORTFILE Import numeric data from a text file as column vectors. %
[DATETIME2142017602PM,VARNAME2] = IMPORTFILE(FILENAME)
however those are actually different functions because the case of the letters is different and MATLAB is case sensitive. Where is it defined?
The apparent use of IMPORTFILE was because the code was not formatted :(
The file might have been present before in the current directory, but that does not mean that it is present now in any directory, let alone the current directory.

Sign in to comment.

the fileId is -1

2 Comments

Change the line
fileID = fopen(filename,'r');
to
[fileID, msg] = fopen(filename,'r');
if fileID < 0
error('Failed to open file "%s" because: "%s"', filename, msg);
end
lol i ended up going into the file directory and changing them to .txt and it worked but thanks for the help

Sign in to comment.

Tags

Asked:

on 18 Feb 2017

Commented:

on 19 Feb 2017

Community Treasure Hunt

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

Start Hunting!