Why does xlswrite not replace file when I tell it to?

조회 수: 10 (최근 30일)
MCM
MCM 2015년 11월 2일
답변: Walter Roberson 2015년 11월 2일
I have a script that uses xlswrite to export some output. I use uiputfile to prompt my user for the filename and path for this output. If a user chooses an existing file, they are ask if they want to replace the existing file.
One of my users noticed that even through he selects "replace existing file", the file is not replaced. Instead the new output is written over what was already in the file. Which creates a big old mess.
I told him the simple fix is to just delete the file ahead of time and recreate it when the script prompts him. Or just use unique filenames. He wasn't impressed with this fix and wants me to make Matlab do it for him. (My boss, so have to).
Anyone know how to do this?

채택된 답변

Walter Roberson
Walter Roberson 2015년 11월 2일
uiputfile() only allows the name to be selected; it does not delete the existing file or set any flags so that routines that write files will know to overwrite the file. The code after the uiputfile() needs to delete (or rename) any existing file if the file writing code updates parts of the file.
[filename, pathstr] = uiputfile('Where do you want to write the results?');
outputfile = fullfile(pathstr, filename);
if exist(outputfile, 'file');
delete(outputfile);
end

추가 답변 (1개)

TastyPastry
TastyPastry 2015년 11월 2일
You could fill the file with empty cells, or there's a solution here. Scroll down to the solution by ImageAnalyst.

카테고리

Help CenterFile Exchange에서 MATLAB Report Generator에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by