Why am I receiving a "file may be corrupt" error when saving my file using "uiputfile"?
39 views (last 30 days)
Show older comments
MathWorks Support Team
on 27 Nov 2023
Edited: MathWorks Support Team
on 4 Jun 2024
I use "uiputfile" in an application that I created using MATLAB R2020b to allow users to select a folder to subsequently use the "save" command and save .MAT files into the selected folder. Now the user is trying to use my code to write files onto a write only file system (i.e., deletion or modification of files is prohibited). The save command threw the following error:
Error using save.
Unable to write to MAT-file Q:\MyFiles\test.mat.
The file may be corrupt.
Upon debugging I saw that "uiputfile" creates a file with size 0 kb on the write only file system. Therefore, the subsequent save could not be performed. Is there a workaround for this issue?
The following is the sample code that I am currently using for my work-
[tempFileName, tempPathName] = uiputfile('*.mat', 'Save file', 'tempFileName');
MyTempFullName= fullfile(tempPathName, tempFileName);
if exist(MyTempFullName, 'file')
save(MyTempFullName, 'data', '-append')
else
save(MyTempFullName, 'data')
end
Accepted Answer
MathWorks Support Team
on 14 May 2024
Edited: MathWorks Support Team
on 4 Jun 2024
The "uiputfile" command does not create new files and only returns the file name and the location of the directory once the user selects a folder to save their files into using the dialog window. You can use the filename and folder path with the "save" function to perform the operation of actually saving files to that location.
While it is true that "uiputfile" does not directly create a file, the native file chooser API that it calls does. Since there are so many different types of file systems and rules that can be on them, APIs like this may test if they can create a file by in fact creating it and then immediately deleting it. In this case, the deletion of the test file failed resulting in a 0 kb file being created.
If you try to use the "save" function with the "-append" flag to write to a 0 kb file, it will throw an error saying either the file does not exist or that the file may be corrupt. To work around this issue, consider these approaches.
Approach 1: Using “uiputfile”
If “uiputfile” pre-creates a 0 kb file for you, the file can be overwritten but cannot be appended to. If there is a conditional statement that appends to a 0 kb file, MATLAB will throw the corrupt file error. This can be alleviated using a "try-catch" statement. First, get a file name and folder path using "uiputfile".
x = 5;
y = 10;
[tempFileName, tempPathName] = uiputfile('*.mat', 'Save file', 'testFile');
With the try-catch statement, if "save('-append')" errors out, regardless of whether it is from no file being found or believing a 0 kb file is corrupt, the code will automatically resort to using the "save" command that creates a new file.
try
save(fullfile(tempPathName,tempFileName), 'y', '-append');
catch
save(fullfile(tempPathName,tempFileName), 'x');
end
Approach 2: Avoiding “uiputfile”
Here, the save location can be selected by selecting the folder into which you to save the MAT file. The equality test checks for an exact file match since "exist()" can return multiple values based on the match. First, get the folder path using "uigetdir".
x = 5;
y = 10;
% Open a dialog that lets the user select a folder path
selpath = uigetdir
% Set the file path for the hypothetical MAT file
matFile = fullfile(selpath,'testFile.mat');
Next, check if the file path exists. If it does, append to it. If not, make a new one.
if exist(matFile) == 2
save(matFile, 'y', '-append')
else
save(matFile, 'x')
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Workspace Variables and MAT Files 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!