Loading multiple data files into multiple structures using a for loop: How can each structure have a unique name?
Mostra commenti meno recenti
I'm a programming and Matlab novice, so if this doesn't make sense I am happy to try and clarify.
I have read extensively on what a terrible idea it is to create dynamic variable names using the eval function or for loops, but I believe my situation is different. I have a folder containing a bunch of folders, each of which is named after a specific site where data was collected. Within each site folder or 4-8 text files, each of which contains a different type of data for that site. I've written a function that takes the path name of the directory as input, and loads each of the data files into a structure for the site. I then wrote a for loop that will run the code for all of the sites in the folder.
My issue: How can I give each structure the site name? The mat file is named after the site, but the mat file for each site contains a structure with the same generic name as all the other sites. (i.e. anyname.mat is a 1X1 structure containing a structure called genericdataname). I want to load the mat file and have it load as a structure with the name of the site, so I can just call the site data using a command like "sitename.datatype1". Here's my function code:
function [corestruct] = LoadDat(filepath)
paths = strcat(filepath,'*.txt')
files = dir(paths);
fiel = ['benthic_';'planktic';'sst_____';'hmm_____';'bacon___';'pa______';'sa______'];
corestruct = struct(fiel(1,:),[],fiel(2,:),[],fiel(3,:),[],fiel(4,:),[],fiel(5,:),[],fiel(6,:),[],fiel(7,:),[]);
for i = 1:length(files)
pathname = strcat(filepath,files(i).name);
data = load(pathname);
if isempty(strfind(pathname,'_benth'))==0
teststruct.benthic_ = data;
elseif isempty(strfind(pathname,'_plank'))==0
corestruct.planktic = data;
elseif isempty(strfind(pathname,'_sst'))==0
corestruct.sst_____ = data;
elseif isempty(strfind(pathname,'_hmm'))==0
corestruct.hmm_____ = data;
elseif isempty(strfind(pathname,'_ba'))==0
corestruct.bacon___ = data;
elseif isempty(strfind(pathname,'_pa'))==0
corestruct.pa______ = data;
else
corestruct.sa______ = data;
end
end
And this is the code for my loop that runs the function for all of the sites and saves the structure as a mat file:
PATH = 'pathname/pathname/pathname;
mydatums = dir(PATH);
for i = 4:length(mydatums)
dirpath = strcat(PATH,mydatums(i).name,'/')
coredata = LoadDat(dirpath)
save(mydatums(i).name,'coredata')
end
It is the fact that the loop produces only the one variable (coredata) and that variable is a structure that is then saved to the mat file. If there was some way to change the name of the coredata variable to match the site name with each loop iteration that would solve the problem, but I have no idea how to do that.
1 Commento
Sebastian Castro
il 17 Apr 2017
There are so many ways to do this, which depends on how you want the data to be formatted:
- Do you want coredata to be a structure (which happens when you call load like that), or do you want it in the same format it was in originally?
- Do you want to save multiple MAT-files with one variable each, or one MAT-file with all the data in it?
- If you want one file, do you want separate variables or a single array/structure containing all the loop iterations' data?
Risposte (0)
Categorie
Scopri di più su Workspace Variables and MAT Files in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!