Code failure on recent version of MATLAB

1 view (last 30 days)
Hello, I wondered if anyone could help me with a piece of code that works perfectly well on MATLAB 2017, but when updating to R2019a, it fails. The particular region where it breaks down is:
formatSpec = '%f%[^\n\r]';
fileID = fopen(filename,'r');
delimiter = {''};
startRow = 2;
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'TextType', 'string', 'HeaderLines' ,startRow-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
fclose(fileID);
Retured error states problem with textscan ("Invalid file identifier. Use fopen to generate a valid file identifier"), despite the code working in earlier version.
For background, the script will import a text file (almost always .csv) with arbitrary fluorescent values associated with frequency points, followed by formatting, removal of values if required and plotting etc.
Hope that's enough info and thank you :)
  1 Comment
Guillaume
Guillaume on 28 Jan 2020
What is the value of filename? Is it an absolute or relative path?

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 28 Jan 2020
So, it sounds like filename is a relative path or a just a filename. Either way, it relies on the file being in the current folder, or in a folder on matlab's path. The reason it would be working in one matlab installation and not another is probably simply that the file folder has been added to matlab's path on one system (with addpath) and not on the other, or maybe the current directory is different.
One way to solve the problem would be to add the required folder to matlab's path. I wouldn't recommend that. Folders on matlab path should be code folders not data folder. For data, it is much safer to use absolute paths which means that the data can reside anywhere without having to change a thing. We don't know how filename is constructed, if it's an input you pass the function:
folderwherethefileis = 'C:\somewhere\somefolder'; %make sure that it is absolute. On windows, it means it starts with a drive letter
filename = fullfile(folderwherethefileis, 'somefilename.csv');
%... you now have a absolute path to the file. fopen will succeed unless the path is wrong or you don't have permission
fileID = fopen(filename,'r');
While we're at it, I suggest you modify that fopen line, to check that it succeeds and if not tells you why.
[fileID, errmsg] = fopen(filename,'r');
assert(fileID > 0, 'Failed to open %s. The error is: %s', filename, errmsg);

More Answers (1)

Stephen23
Stephen23 on 28 Jan 2020
Edited: Stephen23 on 28 Jan 2020
An empty permission string is not supported by the documentation:
fopen(filename,'');
% ^^ Not supported by the FOPEN documentation.
I doubt that it was ever supported. You should use a permission string that follows the documentation.
  3 Comments
Guillaume
Guillaume on 28 Jan 2020
Please do answer my question. In particular, if filename is a relative path, is the folder where the data is located the current folder or a folder that is supposed to be on matlab's path?
Thomas Bateman-Price
Thomas Bateman-Price on 28 Jan 2020
Hi Guillaume,
I was just double checking before I answered, I am relatively new to MATLAB and was unsure initially what you meant so please accept my apologies. I believe it is a relative path - after changing the location of the data file to the working directory, the script ran, which is great... However, I assume there is a way to read in such text files from an arbitary location other than the WD?
Thank you again for your responces and help!
Tom

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!