Is it possible to code my 'data loading' in a less sloppy / more automated way?
2 次查看(过去 30 天)
显示 更早的评论
Hi Everyone, I am currently loading and saving excel files, first into my workspace and then into .mat files for later use. The amount of data I have to save is really big, and I will very likely have to do this more often as it need to be done for every new set of measurements we do. This is why I am wondering if there is any shorter or more automated way to write it down. I have to repeat this for 13 patches for every inclusion. In the end I already have a loop to save it all. Thanks in advance to anyone that can help! If it is possible...
This also is a patch with onyl two measurements, sometimes the are 9 measurements leading to a total of 36 files to load.
At the moment I do something like this:
%% patch 6
f_6 = ["S6_M1_1039_O2norm.xlsx", "S6_M2_1147_O2norm.xlsx",...
"S6_M1_1039_O20.xlsx", "S6_M2_1147_O20.xlsx"] ;
%O2norm
S1_Patch6M1_O2norm_630nm = zeros(4000,5);
S1_Patch6M2_O2norm_630nm = zeros(4000,5);
S1_Patch6M1_O2norm_670nm = zeros(4000,5);
S1_Patch6M2_O2norm_670nm = zeros(4000,5);
for i = 2:1:6
S1_Patch6M1_O2norm_630nm(:,i-1) = xlsread(f_6(1), i, 'A2:A4001');
S1_Patch6M2_O2norm_630nm(:,i-1) = xlsread(f_6(2), i, 'A2:A4001');
S1_Patch6M1_O2norm_670nm(:,i-1) = xlsread(f_6(1), i, 'B2:B4001');
S1_Patch6M2_O2norm_670nm(:,i-1) = xlsread(f_6(2), i, 'B2:B4001');
end
%O20
S1_Patch6M1_O2norm_630nm = zeros(4000,5);
S1_Patch6M2_O2norm_630nm = zeros(4000,5);
S1_Patch6M1_O2norm_670nm = zeros(4000,5);
S1_Patch6M2_O2norm_670nm = zeros(4000,5);
for i = 2:1:6
S1_Patch6M1_O2norm_630nm(:,i-1) = xlsread(f_6(3), i, 'A2:A4001');
S1_Patch6M2_O2norm_630nm(:,i-1) = xlsread(f_6(4), i, 'A2:A4001');
S1_Patch6M1_O2norm_670nm(:,i-1) = xlsread(f_6(3), i, 'B2:B4001');
S1_Patch6M2_O2norm_670nm(:,i-1) = xlsread(f_6(4), i, 'B2:B4001');
end
4 个评论
dpb
2022-5-12
编辑:dpb
2022-5-12
Carrying on from the above...
%% patch 6
f_6 = ["S6_M1_1039_O2norm.xlsx", "S6_M2_1147_O2norm.xlsx",...
"S6_M1_1039_O20.xlsx", "S6_M2_1147_O20.xlsx"] ;
Start by making sure your file-naming convention is searchable by wildcard matching -- it appears it may be as is if the above "S6_" prefix is ID for set 6. If that is the case, I'd strongly suggest also using something like "S00006_" by a "%06d" width field and the preceeding zero fill to handle a large number of measurement sets. Doing this will also mean the files will sort contiguously by number which can be a big help. You might also want to consider some sort of directory structure for storing the files so that you don't end up with 10,000 files in one folder. By week or month could be one way; but any organizing method would work as long as it is logical and sorts (which means if use dates, use the YYMMDD numeric order also with leading zeros, not MMM/DD/YY, for example).
With that, you can begin to turn the above code into a generic function by using
Set=6; % set/ask user for measurement set of interest
ROOT_DIR="C:\BaseDataPath"; % if fixed or use uigetdir or ...
DATASET="S"+Set; % take advantage MATLAB string syntax to catenate number to string
WILDSTR="_*.xlsx"; % the wildcard pattern to match file naming convention
d=dir(fullfile(root_data_path,strcat(DATASET,WILDSTR)); % and return all those matching files
for i=1:numel(d)
tData=readtable(fullfile(d(i).folder,d(i).name)); % and read the file
...do whatever with each file here...
end
Would need more details on just what is content and need going forward, but either add the metadata as data in the table or use a struct or some other similar organizing storage instead of creating files with such data in the names.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!