MATLAB Answers

How to print same input file name as the output file name

34 ビュー (過去 30 日間)
Sara
Sara 2021 年 1 月 17 日
コメント済み: Sara 2021 年 1 月 18 日
Hi,
I am working on analyzing text files. I used fopen to open the txt file as following:
fid = fopen('output file name','w');
table = [t1(:),t2(:),t3(:)];
formatSpec ='%s,%1.1f,%1.1f,%1.1f\n';
for i= 1:length(x)
fprintf(fid,formatSpec,s{i,:}',table(i,:));
end
fclose(fid);
The code above is part of my code, which is the part that I use to print the output. The result of this code will print the output file name in my current folder.
How can I make the output text file name the same name as the input file name? Instead of typing the text file manually and sometimes I forget to change the output file's name.

  0 件のコメント

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2021 年 1 月 17 日
編集済み: Adam Danz 2021 年 1 月 17 日
How are you getting the input file name in the first place? If it's stored as a variable, use that variable to name the output file.
fname = 'output file name';
fid = fopen(fname,'w');
or with file extension
fname = 'output file name';
fid = fopen([fname,'.txt'],'w');

  2 件のコメント

Sara
Sara 2021 年 1 月 18 日
Thank you for your answer.
I used the extension line in my code and It works as I want it
the input file loaed it as follwoing code
filename = uigetfile('*.*');
input = importdata (filename)
Image Analyst
Image Analyst 2021 年 1 月 18 日
You need to put it in a different folder or else you'll overwrite your input file with your output file since it has the same name! If you didn't do this, then your input file is now toast. It will have whatever you wrote to your output file.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2021 年 1 月 18 日
Try this:
[inputFolder, inputBaseFileNameNoExt, ext] = fileparts(fullInputFileName);
outputFolder = fullfile(inputFolder, '/Output files'); % Wherever you want.
if ~isfolder(outputFolder)
% Folder does not exist so create it.
mkdir(outputFolder);
end
% Output file uses the same name as the input file, it's just in a different folder.
fullOutputFileName = fullfile(outputFolder, [inputBaseFileNameNoExt, ext]);
fid = fopen(fullOutputFileName, 'wt'); % Use wt to open for writing in text mode.
% Code below is the same as yours. I hope it works.
table = [t1(:),t2(:),t3(:)];
formatSpec ='%s,%1.1f,%1.1f,%1.1f\n';
for i= 1:length(x)
fprintf(fid, formatSpec,s{i,:}',table(i,:));
end
fclose(fid);

  4 件のコメント

表示 1 件の古いコメント
Walter Roberson
Walter Roberson 2021 年 1 月 18 日
outputFolder = fullfile('path/to/the/Output files'); % Wherever you want.
That is, put in the directory where you want the output files to go.
It is not good practice to overwrite an input file: if something went wrong you would have a corrupted file.
And besides, the place you are getting input from is read-only so you cannot write there.
Image Analyst
Image Analyst 2021 年 1 月 18 日
You must specify a folder that you can write to. Evidently you specified a system folder that you cannot write to. You MUST use a different folder for the output file since you said that it was going to have the same name as the input folder and if you don't, then your output file will blast on top of your input file and destroy it.
Sara
Sara 2021 年 1 月 18 日
Thank you guys so much for your comment. It is clear now to me.
I actually save the output file to a folder on my desktop instead of my MATLAB cloud. I added the code from the previous answer to your code to control my file's extension because some of my files' output is in x y z format instead of text format.
fid = fopen([fullOutputFileName, '_Ga.xyz'],'wt');

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by