How to Load and Rename a txt File
조회 수: 3 (최근 30일)
이전 댓글 표시
Hello, I am a Korean university student working on a project using MATLAB GUI. I have a question. If you look at the image below, there are "Load" and "Check" buttons. If you press the "Load" button, you have to load the txt file in any file path. If you press the "Check" button, the loaded txt file must be saved as the file name I specified. I am unable to proceed with this. Please lend me your wisdom. Thank you.
댓글 수: 0
답변 (1개)
Walter Roberson
2022년 5월 28일
use uigetfile to get the file name and directory. Check whether the file name variable is numeric: if it is then the user cancelled. Use fullfile() to put the file name and directory together to create a fully qualified file name.
Use uigetfile in a way similar to uigetfile in order to find out where the user wants to write the file.
Use copyfile()
댓글 수: 2
Walter Roberson
2022년 5월 28일
basenamein = 'custom.txt';
basenameout = 'Message_Data.txt';
userProfile = getenv('USERPROFILE');
desktop = fullfile(userProfile, 'Desktop') ;
csc = fullfile(userProfile, 'CSC';
infile = fullfile(desktop, basenamein);
outfile = fullfile(csc, basenameout);
copyfile(infile, outfile);
This does not have much of a connection to your original question; it has to worry about where to find the user Desktop, and about where the CSC folder is.
This code to find the desktop is not very robust, as it is common for the user profile to point to Application Roaming, which is not certain to even be on the same disk as the user desktop. The code to reliably find the desktop needs a fair bit of improvement.
.... whereas if you had used uigetfile like I recommend, then the directory returned will be where the file really is, with the user selecting the directory that suits their needs.
참고 항목
카테고리
Help Center 및 File Exchange에서 Search Path에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!