How to Load and Rename a txt File

1 次查看(过去 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.

回答(1 个)

Walter Roberson
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 个评论
건희 이
건희 이 2022-5-28
Thank you. For example, if you have a 'custom.txt' file on your desktop, how would you code it if you wanted to save it as 'Message_Data' in a folder called 'CSC'? I'm not familiar with MATLAB, so I don't know how to write the code. I'd appreciate it if you could help me one more time.
Walter Roberson
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 CenterFile Exchange 中查找有关 Desktop 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by