I need to extract some data from .CSV files that resides in their own directory. This directory has 4 folders with about 200 sub folders each. Within these sub-folders there are anywhere from 2-10 .csv files. I got my script to scan through these folders and extract information that I need from them, but now I need to save my PIN array as a .mat file, which is not too difficult. The hard part that I can't figure out is how to save the .mat files with the same name as the .csv files and in sub-folders with the same name as they were accessed from, but in a different directory. My code is attached below.
if true projectdir = 'C:\Users\it58528\Documents'; %Start here. or name an absolute directory
folderinfo = dir(projectdir);folderinfo = folderinfo([folderinfo.isdir]); %select only the directories
folderinfo = folderinfo(~ismember({folderinfo.name}, {'.', '..'})); for folderidx = 1 : length(folderinfo)thisfolder = fullfile(projectdir, folderinfo(folderidx).name);subfolderinfo = dir(thisfolder);subfolderinfo = subfolderinfo([subfolderinfo.isdir]); %select only the directoriessubfolderinfo = subfolderinfo(~ismember({subfolderinfo.name}, {'.', '..'})); %remove directories . and ..
for subfolderidx = 1 : length(subfolderinfo) thissubfolder = fullfile(thisfolder, subfolderinfo(subfolderidx).name); fileinfo = dir( fullfile(thissubfolder, '*.csv') ); for fileidx = 1 : length(fileinfo) thisfile = fullfile(thissubfolder, fileinfo(fileidx).name); [filepath, basename, ext] = fileparts(thisfile); data = csvread(thisfile,5,2); PIN(fileidx).PIN = fileinfo(fileidx).name(1:17); PIN(fileidx).loadprofile = data(1:15,:); PIN(fileidx).hours = sum(sum(PIN(fileidx).loadprofile,1)); PIN(fileidx).loadprofilepercent = PIN(fileidx).loadprofile./PIN(fileidx).hours; PIN(fileidx).loadpercent = data(:,2); PIN(fileidx).RPM = data(16,:); loadprofilecolumn = find(PIN(fileidx).RPM>Resonance); xSpeed = PIN(fileidx).RPM(loadprofilecolumn(1)-1); PIN(fileidx).TimeatLevel = Test_Goal*PIN(fileidx).loadprofilepercent(:,loadprofilecolumn(1)-1); PIN(fileidx).TestTime = PIN(fileidx).TimeatLevel./((TestLevel./Modeled_Accel).^m); PIN(fileidx).TotalTestTime = sum(PIN(fileidx).TestTime); PINs(fileidx,1:17) = PIN(fileidx).PIN; FieldHours(fileidx,1) = PIN(fileidx).hours; TestHours(fileidx,1) = PIN(fileidx).TotalTestTime; end %files within subfolder
end %subfolders within folder
Best Answer