Errors related to macOS version

3 vues (au cours des 30 derniers jours)
Frank O'Donnell
Frank O'Donnell le 28 Fév 2023
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?

Réponse acceptée

Walter Roberson
Walter Roberson le 28 Fév 2023
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 commentaires
Walter Roberson
Walter Roberson le 3 Mar 2023
[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);
Frank O'Donnell
Frank O'Donnell le 4 Mar 2023
Thank you! That works.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by