How to save a structure as .mat?

조회 수: 8 (최근 30일)
Ibro Tutic
Ibro Tutic 2015년 10월 27일
댓글: Image Analyst 2020년 12월 26일
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 directories
subfolderinfo = 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
  댓글 수: 2
Rik
Rik 2020년 12월 26일
Unfortunately for you Google cache often contains the originally posted question. I used it to revert your edit. Please don't edit away content. Why are you on a mass-deletion campaign? The only thing you're achieving is to annoy everybody who would help you with a future question.
Image Analyst
Image Analyst 2020년 12월 26일
Seomtimes I check the prior posts of people. If I see lots of flags for deletions, then I don't answer that person.

댓글을 달려면 로그인하십시오.

채택된 답변

Thorsten
Thorsten 2015년 10월 27일
for subfolderidx = 1 : length(subfolderinfo)
subfolderi = subfolderinfo(subfolderidx).name
thissubfolder = fullfile(thisfolder, subfolderi);
fileinfo = dir( fullfile(thissubfolder, '*.csv') );
% create the new dir
newsubfolder = fullfile(newdir, subfolderi);
mkdir(newsubfolder)
for fileidx = 1 : length(fileinfo)
filenamei = fileinfo(fileidx).name;
thisfile = fullfile(thissubfolder, filename);
[filepath, basename, ext] = fileparts(thisfile);
data = csvread(thisfile,5,2);
newfilename = fullfile(newsubfolder, filenamei);
% save data to newfilename
end
  댓글 수: 1
Ibro Tutic
Ibro Tutic 2015년 10월 27일
편집: Ibro Tutic 2015년 10월 27일
Cool thanks.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by