I need to set a file as an input
2 ビュー (過去 30 日間)
古いコメントを表示
This is a bit complex. I have a .m file that currently opens a .txt file, edits that file, and saves it under a new name. I currently have the file names for both the file that is opened/edited and the file name that the edited code is saved under hard-coded into my .m file. However, I am going to have to use this program with many different .txt files. I would like to not have to change the .m file's code every time I need to use the program with a different .txt file. Is there anyway I could tell MATLAB which file to open and what to save the edited file as from the command window? So essentially I need to set the old file and new file name as a input variable of sorts in my .m file. I would appreciate any help.
1 件のコメント
Matt Kindig
2013 年 6 月 18 日
編集済み: Matt Kindig
2013 年 6 月 18 日
This is actually rather common. Just wrap the file opening, matlab logic, and file saving into a function, i.e.
function out = myfunction(inFile, saveFile)
fid = fopen(inFile, 'rt'); %open your text file for reading
%do matlab stuff here
fid2 = fopen(saveFile, 'wt'); %open save file for writing
fwrite(fid2, ... %or however else you are writing your save file
fclose(fid); fclose(fid2); %close your files.
Then you can just call your function from the command prompt as:
myfunction('myfile1.txt', 'outfile1.txt');
myfunction('myfile2.txt', 'outfile2.txt');
You can even wrap the call to myfunction() in a loop to loop through a bunch of files. See http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Environment and Settings についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!