How to Load and Rename a txt File

1 view (last 30 days)
건희 이
건희 이 on 28 May 2022
Commented: Walter Roberson on 28 May 2022
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.

Answers (1)

Walter Roberson
Walter Roberson on 28 May 2022
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 Comments
건희 이
건희 이 on 28 May 2022
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 on 28 May 2022
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.

Sign in to comment.

Categories

Find more on Desktop 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!