How to organize data by reading specific name structure?
이전 댓글 표시
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
VINAYAK LUHA
2023년 9월 7일
Hi Carola,
What's the basis of pairing ? same day and time ?
Carola Forlini
2023년 9월 7일
Jon
2023년 9월 7일
Do you just want to put the names of the files into this structure, or the contents of the files? If the latter, can you please attach a few typical files, and say something about how you want the contents itself stored within the structure
Carola Forlini
2023년 9월 7일
Jon
2023년 9월 7일
Please provide details out what you would like this structure (array of structures?)to look like
Carola Forlini
2023년 9월 7일
Sorry, still not clear to me. Suppose we call the data in each of your attached files a dataset. Then you will have collections of data sets for each day, within that day you will have collections of datasets for each time, within each time you will have a dataset for each instruments. So exactly what would you like the structure (array of structures) to look like (please provide and example of an element of your "structure" and what fields you would have). By the way I would suggest putting the data into a timetable rather than a structure, as it seems that however you design it, accessing particular elements of such a structure would be more difficult than if the data were in timetable. Here's how you could put that data into a timetable.
% Example of putting your data into timetable
% Get list of all the relevant data files
list = dir('wk*.mat');
% Get the part of the names with the day and hour
filenames = {list.name}'; % cell array of filenames
timestr = extractBetween(filenames,6,12);
% Get the part of the names with the instruments
instrument = extractBetween(filenames,1,4);
% Convert to datetime
time = datetime(timestr,"Format","dd_HHmm");
time.Format = 'dd-HH:mm'; % make display formatting a little more conventional
% Put filenames and instrument name into a timetable
tt = timetable(time(:),filenames(:),instrument(:),...
'VariableNames',...
{'filename','instrument'}); % use (:) to be sure they are columns
% Loop through filenames adding data to the timetable
for k = 1:numel(filenames)
% Load the data from the file
s = load(filenames{k});
tt.fs{k} = s.fs;
tt.sp{k} = s.sp;
tt.ts{k} = s.ts;
end
Carola Forlini
2023년 9월 7일
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
% .
% .
% .
채택된 답변
추가 답변 (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
Carola Forlini
2023년 9월 7일
VINAYAK LUHA
2023년 9월 7일
Hi Carola,
Thanks, I've corrected the code, now it should work fine.
Carola Forlini
2023년 9월 7일
카테고리
도움말 센터 및 File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!