Errors related to macOS version

A friend gave me some MATLAB code written in R2017B on Windows.
It executes normally in R2018A on macOS 10.15 Catalina. However, it produces errors in R2018B, R2019B, R2020B and R2022B on macOS 13.2 Ventura.
The specific errors are:
Error using textscan. Invalid file identifier. Use fopen to generate a valid file identifier.
Error while evaluating UIControl Callback.
I haven't previously run into MATLAB errors that are related to the computer OS version rather than the MATLAB version. Are these likely to be addressed in a pending MATLAB release? Or do I need to look at revising the code to get it to run on the Ventura Mac?

 Accepted Answer

It is difficult to be certain without seeing the code, but my first guess would be:
that the code has a call similar to
[filename, pathname] = uigetfile(some parameters);
fullname = [pathname filename];
That code would fail if the returned pathname from uigetfile is not empty and did not end in a directory separator.
Code similar to the above should always be rewritten more like
[filename, pathname] = uigetfile(some parameters);
fullname = fullfile(pathname, filename);
fullfile() automatically detects whether the given path already ends in a directory separator, and if not then automatically adds one.
(To be more correct, fullfile removes all trailing directory separators from the directory passed in, and then inserts a single copy of the directory separator appropriate to the operating system.)
It is common for people to assume that uigetfile() and uigetdir() return paths that end in a directory separator, but the function has never promised that -- and the function is not guaranteed to be consistent as to whether it provides the separator or not. The answer might be different, for example, for the current directory than for other directories.

3 Comments

Thanks for the suggestions, Walter.
In the code I was given, the error was flagged in this area:
FileName=uigetfile;
fid1=fopen(FileName,'r');
...
fclose(fid1);
This produces the errors noted in my original post.
Making a guess on how your comment might apply in this case, I changed the above to:
FileName=uigetfile;
fid1=fullfile(FileName,'r');
...
fclose(fid1);
Now when the file is run, the initial error no longer appears, but this one does instead:
Error using fclose
Character vector argument must be 'all'.
Error in [section] (line xxx)
fclose(fid1);
Error while evaluating UIControl Callback.
Is the solution now just to add the argument 'all' somewhere on the fclose line?
[FileName, PathName] = uigetfile;
if ~ischar(FileName)
return; %user asked to cancel
end
FileName = fullfile(PathName, FileName);
[fid1, msg] = fopen(FileName, 'r');
if fid1 < 0
error('Failed to open file "%s" because "%s"', FileName, msg);
end
...
fclose(fid1);
Thank you! That works.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!