Help naming text file according to user input

조회 수: 9 (최근 30일)
David (degtusmc)
David (degtusmc) 2013년 7월 1일
댓글: KAE 2024년 4월 30일
Hello everyone,
I am trying to create a text file (using a gui), and I would like the name of the file to match what the user inputs in an edit text box. At first, I am trying to use a simple script (attached below) to see if I can accomplish this, but I am unable to create the .txt file. The file being created is just a "file". What would be the best way to approach this? Am I on the right track?
Thank you in advance for any help provided.
Code:
prompt = 'Please enter the file name: ';
fileName = input(prompt,'s');
% open a file for writing
fid = fopen(fileName, 'wt');
if fid == -1
error('Cannot open file: %s', fileName);
end

채택된 답변

Ahmed A. Selman
Ahmed A. Selman 2013년 7월 1일
Just add the extension you like to the variable (fileName), as
prompt = 'Please enter the file name: ';
fileName = input(prompt,'s');
fileName1=[fileName,'.txt']; % HERE. Choose different extension if you like.
% open a file for writing
fid = fopen(fileName1, 'wt'); % HERE
if fid == -1
error('Cannot open file: %s', fileName1); AND HERE
end
% (HERE indicates changes I've made)
:-)
  댓글 수: 1
David (degtusmc)
David (degtusmc) 2013년 7월 1일
Thank you for the rapid response. When I was trying to add the extension, I was doing it all in one line and I kept getting errors. It makes sense to append it separately. Thank you so much!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sowndarya Sivalingam
Sowndarya Sivalingam 2020년 6월 2일
I want to know how to crete this script: Write a matlab script which will take input for a file name from the command prompt. The script will also contain a function. The function will be called with file name, it will extract the data part, calculate the size of the data, average of the data, maximum value in the data and report back those values to main function.
  댓글 수: 1
KAE
KAE 2024년 4월 30일
Post this as a new question and include what you have tried so far.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by