MATLAB Answers

How to save a structure as .mat?

4 views (last 30 days)
Ibro Tutic
Ibro Tutic on 27 Oct 2015
Commented: Image Analyst on 26 Dec 2020
This question was flagged by Walter Roberson
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 Comments

Rik
Rik on 26 Dec 2020
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 on 26 Dec 2020
Seomtimes I check the prior posts of people. If I see lots of flags for deletions, then I don't answer that person.

Sign in to comment.

Accepted Answer

Thorsten
Thorsten on 27 Oct 2015
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 Comment

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by