Apply functions or m to a folder(multiple files)

3 views (last 30 days)
Az
Az on 14 Sep 2011
I got a folder with say 100 file with the random names of haah.xls,kiij.xls etc. I have a function(say QQQ.m) I want to apply to them which would eventually convert the file to csv. I want to do two things: 1)How to apply QQQ.m to the folder 2)I want to rename the converted file according to its original name.Now the function does this, convert haah.xls to 1.csv. I want it to automatically rename to haah.csv Thanks in advance!!!
  1 Comment
Jan
Jan on 14 Sep 2011
If you post yozur code, it would be easy to explain which single line has to be changed. Without seeing your code, we could only reply a theoretical description of what muist be changed.
So please show us, what you have done already.

Sign in to comment.

Answers (1)

Jan
Jan on 14 Sep 2011
  • Use DIR to get a list of files.
  • Then use a FOR loop to get one file after the other.
  • Read the file. It is impossible to get a more specific instruction, because you did not explain the format of the files.
  • Write the converted file using the same file name, but the different file extension. This is smarter than writing "1.csv" and using MOVEFILE to rename the file afterwards.
If you include more details in your question, a more specific answer is possible.
[EDITED]: You must have the original file name to read the file. So why do you use "1.csv" and not something like:
[fPath, fName] = fileparts(OriginalFileName);
newfileName = fullfile(fPath, [fName, '.csv']);
cell2csv(newFileName, W);
  2 Comments
Az
Az on 14 Sep 2011
Thank you for your help!
I got a folder with file 20010101-20010601.xls all the way to 20110601-20110901.xls, 26 files.but I got several folders.
I got a function to convert any file to what I want but the default name of the output is 1.csv.
Now what I want to do is process all the files under one folder at one time and automatically renamed to 20010101-20010601.csv all the way to 20110601-20110901.csv.
I hope it clearify my question.
My function looks like this:
function W=nav(data,textdata)
K=data(:,2:2:end)
c_size=size(K,2)
l=4000*c_size
NAV=num2cell(reshape(K,l,1))
a=textdata(2,:)
b=a(:,4:2:end)
c=b(:,1:c_size)
DATE=reshape(repmat(c,4000,1),l,1)
ID=num2cell(repmat(data(:,1),c_size,1))
O=horzcat(DATE,ID,NAV)
u={'Date','FundID','Nav'}
W=vertcat(u,O)
LocRemov=find(isnan(cell2mat(W(2:end,3))))+1
W(LocRemov,:)=[]
cell2csv('1.csv',W)
end
Thank you so much
Jan
Jan on 14 Sep 2011
Please insert the code to your original question.

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!