Failure in script to import data automaticaly

2 views (last 30 days)
Dear all,
I am trying to import data through a piece of code I modified. I am using MATLAB2016b. I’ve included the dataset, achieving the output and the variable dataset.
I’m sorry to bother you guys again, I’m fairly new to MATLAB and only using the book Matlab: an introduction to programming and problem solving by Stormy Attaway.
This are the errors:
Error using textscan Invalid file identifier. Use fopen to generate a valid file identifier.
Error in import (line 33) dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'EmptyValue' ,NaN, 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
I’ve tried really anything, and sometimes there pop ups another error.
I thank you all in advance.
I attached the: .csv file, a screen of the dataset as it should be and a screen of the workspace.
The code:
%%Import data from textfile.
% Script for importing data from the following text file:
%
%
% To extend the code to different selected data or a different text
% file,generate a function instead of a script.
%%Initialize variables.
filename = 'apx.csv';
delimiter = ',';
startRow = 2;
%%Format string for each line of text
% column1: text (%q)
% column2: double (%f)
% column3: double (%f)
% column4: double (%f)
% column5: double (%f)
% column6: double (%f)
% column7: double (%f)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%q%f%f%f%f%f%f%[^\n\r]';
%%Open the textfile.
fileID = fopen(filename,'r');
%%Read columns of data according to format string.
% 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.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'EmptyValue' ,NaN, 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
%%Close the text file.
fclose(fileID);
%%Post processing for unimportable data.
% No unimportance 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.
%%Create out variable
apx = dataset(dataArray{1:end-1}, 'VarNames', {'Date','Open','High','Low','Close','Volume','AdjClose'});
%%Clear temporary variables
clearvars filename delimiter startRow formatspec fileID dataArray ans;

Answers (1)

ES
ES on 28 Jun 2017
Does the file exist? the error says it could not find the file. Try giving the full path. or check if the file exists using
exist(filename,'file')
  3 Comments
ES
ES on 28 Jun 2017
Your AXP variable has the data you need. correct?
F. Bondeau
F. Bondeau on 28 Jun 2017
It does but not in the correct matter as I attached in my op: Output.png.

Sign in to comment.

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!