How to organize data by reading specific name structure?
2 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I have multiple pairs of mat files named like "wk65_10_1044" or "wk85_10_1044" where:
- wk65/wk85 = instrument;
- 10 = day;
- 1044 = time.
Is there a way I can organize a code where after specifying instrument, day and time, it will check all the mat files and put the pairs under the same structure?
Thank you
10 件のコメント
Jon
2023 年 9 月 8 日
Generally preferable to use some kind of array to handle this type of data rather than embedding the array indexing in a field name, e.g. time1, time2. Much harder to too loop through data or access an individual one if you have to manipulate strings to build up field names. You could use something like
data(1).day = 10
data(1).time = 1044
data(1).instrument(1).name = 'wk65'
data(1).instrument(1).fs = fs % using corresponding data for this data set
data(1).instrument(1).sp = sp
data(1).instrunent(1).ts = ts
data(1).instrument(2).name = 'wk85'
data(1).instrument(2).fs = fs
% .
% .
% .
data(2).day = 12
data(2).time = 1333
% .
% .
% .
採用された回答
VINAYAK LUHA
2023 年 9 月 8 日
Hi Carola,
I've made changes as per the said requirements, find the code attached below-
How to follow ?
- Create a folder say A , and place your "rawData" folder in it, also create a new folder there and rename it as "organized".
- With your current path set to A, run this code.
- You can access the data as follows,
- Yes this does cover the case when data is recorded multiple times during the same day.
data.D14.T0829.Iwk85.fs/sp/ts
files=dir(fullfile(rawDataFolderPath,'*.mat'));
for i=1:numel(files)
filename= files(i).name;
splitfilename=split(filename,"_");
compInstrument =cell2mat(splitfilename(1));
compDay =cell2mat(splitfilename(2));
compTime=cell2mat(splitfilename(3));
compTime=compTime(1:end-4);
organize("organized",[string(compDay),string(compTime),string(compInstrument)],1);
end
data=getsubs("organized",1);
%Recursive code to organize files in "rawData" folder into folders and subfolders in "organized" folder
function organize(spath,fname,i)
if(i==4)
copyfile("rawData"+"/"+fname(3)+"_"+fname(1)+"_"+fname(2)+".mat",spath+"/");
return
end
if(exist(spath+"/"+fname(i), 'dir') ~= 7)
mkdir(spath+"/"+fname(i))
end
organize(spath+"/"+fname(i),fname,i+1)
end
%Recursive code to convert folder directory setup to MATLAB structure
function root= getsubs(spath,i)
contents = dir(spath);
contents = contents(~ismember({contents.name}, {'.', '..'}));
if(numel(contents)==1&&contents.isdir==0)
temp = load(spath+"/"+contents.name);
root = temp;
return
end
root =struct();
for j =1:numel(contents)
subs= getsubs(spath+"/"+string(contents(j).name),i+1);
key = string(contents(j).name);
if(i==1)
root.("D"+key)=subs;
elseif(i==2)
root.("T"+key)=subs;
elseif(i==3)
root.("I"+key)=subs;
end
end
end
Hope this helps
その他の回答 (1 件)
VINAYAK LUHA
2023 年 9 月 7 日
編集済み: VINAYAK LUHA
2023 年 9 月 7 日
Hi Carola,
Specify the qDay and qTime values in the below code, and access the data like data.wk65.fs/sp/ts , data.wk85.fs/sp/ts
qDay ='14';
qTime='0829';
qAns = [];
qInstrument=[];
files=dir(fullfile(EnterFolderPathHere,'*.mat'));
for i=1:numel(files)
filename= files(i).name;
splitfilename=split(filename,"_");
compDay =cell2mat(splitfilename(2));
compTime=cell2mat(splitfilename(3));
compTime=compTime(1:end-4);
if(strcmp(compDay, qDay) && strcmp(compTime, qTime))
qAns =[qAns;string(filename)];
qInstrument=[qInstrument,string(cell2mat(splitfilename(1)))];
end
end
data = struct(qInstrument(1),load(qAns(1)),qInstrument(2),load(qAns(2)));
Hope this helps
3 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!