I need to set a file as an input
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Matt Kindig
le 18 Juin 2013
Modifié(e) : Matt Kindig
le 18 Juin 2013
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
Réponses (0)
Voir également
Catégories
En savoir plus sur Environment and Settings dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!