How to open multiple files

I think I posted this question in the wrong place earlier. I searched the Answers but didn't see anything that helped me.
I have code that opens a file, modifies the file, then saves the file with a new extension. I would like to be able to select multiple files in a directory, modify each one and then save each one with the new extension.
Here is my code to open a single file:
DefaultFile='';
[oldFileName,PathName] = uigetfile('*.txt','Select File to Modify',DefaultFile);
fid=fopen(fullfile(PathName,oldFileName));
Here is my code to save the file with a new extension:
[folder, oldBaseName, oldExt] = fileparts(oldFileName);
newBaseName = sprintf('%s.rsp',oldBaseName);
newFullFileName = fullfile(folder, newBaseName);
fileID = fopen(newFullFileName,'w');
fprintf(fileID,'%15s\r\n','# MHz Y RI R 50');
fprintf(fileID,'%6.2f %12.8f %12.8f\r\n',dataExport);
fclose(fileID);
So, how do I create a loop to be able to: 1. Select multiple files (number of files will vary each time I run this). 2. Modify my file (I have the code to do that). 3. Save the modified file with the new extension (see code above). 4. Repeat steps 2 and 3 until all files have been modified.
Thanks.

 Réponse acceptée

Walter Roberson
Walter Roberson le 12 Déc 2012

0 votes

Use uigetfile() with 'MultiSelect', 'on'. Then a cell array of strings is returned for the file name. You can loop through that.

5 commentaires

AsYouWish
AsYouWish le 12 Déc 2012
I am a a newbie and don't know where to find "Multiselect". Thanks.
AsYouWish
AsYouWish le 12 Déc 2012
Think I found it.
[oldFileName,PathName] = uigetfile('*.dat','Select the dat-files', 'MultiSelect','on');
AsYouWish
AsYouWish le 12 Déc 2012
So I ran the code and I can open the cell array and see that all files have been read in but I am not sure how to cycle through each file with the loop. Any tips?
Thanks.
[oldFileNames,PathName] = uigetfile('*.dat','Select the dat-files', 'MultiSelect','on');
for K = 1 : length(oldFileNames)
oldFileName = oldFileNames{K};
[....]
end
AsYouWish
AsYouWish le 12 Déc 2012
Thanks. That works great!

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 12 Déc 2012

0 votes

1 commentaire

AsYouWish
AsYouWish le 12 Déc 2012
I downloaded the dll and will give it a try this morning. Thanks.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by