How to save a structure as .mat?

8 visualizaciones (últimos 30 días)
Ibro Tutic
Ibro Tutic el 27 de Oct. de 2015
Comentada: Image Analyst el 26 de Dic. de 2020
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 comentarios
Rik
Rik el 26 de Dic. de 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 el 26 de Dic. de 2020
Seomtimes I check the prior posts of people. If I see lots of flags for deletions, then I don't answer that person.

Iniciar sesión para comentar.

Respuesta aceptada

Thorsten
Thorsten el 27 de Oct. de 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 comentario
Ibro Tutic
Ibro Tutic el 27 de Oct. de 2015
Editada: Ibro Tutic el 27 de Oct. de 2015
Cool thanks.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by