MATLAB: Is MATLAB 7.0.4 (R14SP2) unable to recognize the files, when I use the COPYFILE command in the script

addpathchangecopyfileMATLABnotificationpathrelativetype

I run a script, myfile.m, which contains the following code:
FullPathTempDir = tempname;
[temp_dir temp_file] = fileparts(FullPathTempDir);
mkdir(FullPathTempDir);
destinationFile = [temp_file,'.m'];
origPath = addpath(FullPathTempDir);
copyfile('hSampleDisp.m',fullfile(FullPathTempDir,destinationFile));
type(temp_file);
I receive the following error :
??? Error using ==> type
tp103239 is a directory.
Error in ==> myfile at 7
type(temp_file);

Best Answer

This bug has been fixed in Release 14 Service Pack 3 (R14SP3). For previous product releases, read below for any possible workarounds:
This has been verified as a bug in MATLAB 7.0.4(R14SP2) in the way COPYFILE notifies MATLAB about the changes made to a destination directory.
The followings are some workarounds to resolve the issue:
1. Using an absolute path name:
Use an absolute path name as an input to functions(TYPE in this case) accessing the destination directory as follows:
FullPathTempDir = tempname;
[temp_dir temp_file] = fileparts(FullPathTempDir);
mkdir(FullPathTempDir);
destinationFile = [temp_file,'.m'];
origPath = addpath(FullPathTempDir);
copyfile('hSampleDisp.m',fullfile(FullPathTempDir,destinationFile));
type(fullfile(FullPathTempDir,destinationFile));
2. Use WHICH or EXIST on the path of the destination directory as follows:
FullPathTempDir = tempname;
[temp_dir temp_file] = fileparts(FullPathTempDir);
mkdir(FullPathTempDir);
destinationFile = [temp_file,'.m'];
origPath = addpath(FullPathTempDir);
copyfile('hSampleDisp.m',fullfile(FullPathTempDir,destinationFile));
which temp_file
type(temp_file);
3. CD to the directory, in which the file is located, before accessing the file.