How to save a structure as .mat?

6 ビュー (過去 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 Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by