Indexing Large Numbers of Data by Name

7 次查看(过去 30 天)
Hi! So I'm trying to write a software for gathering data from tensile tests. I'm fine writing the code for each individual test results, but I'm struggling to find a way to index large amounts of data. Basically, I'm going to recieve an xlsx file for each test named TestRun2_1_29_2021 or TestRun3_1_29_2021, and I will have somewhere around 150 of these tests to run. Each xlsx file contains two column vectors- one for stress, and one for strain. I currently am trying to use a structure array to save the vectors, but I don't know how to navigate through the array.
%First I'm loading the full results, which consists of (for this brief example) 7 different tests
data=load('TensionTest1_19_21.mat');
% Ideally I could navigate through this structure with some simple for loop like
for i=1:length(data)
plot(dataAinT(i),dataEssT(i))
end
%etc but I don't know how to create indexable variables like that.
If anyone has any ideas how to load ~150 tests through a for loop and just have some loop run all the calculations, I'd really appreciate it.
Again, my primary concern is I don't have any clear understanding how to create a structure of indexable vectors, such as the structure array I have now.

回答(1 个)

Walter Roberson
Walter Roberson 2021-3-13
编辑:Walter Roberson 2021-3-13
dinfo = dir('TestRun*.xlsx');
filenames = {dinfo(K).name};
numfiles = length(filenames);
all_data(numfiles,1) = struct(); %preinitialize struct
for K = 1 : numfiles
thisfile = filenames{K};
[~, basename, ~] = fileparts(thisfile);
data = readmatrix(thisfile);
all_data(K).dataAinT = data(:,1);
all_data(K).dataEssT = data(:,2);
all_data(K).name = basename;
end
for K = 1 : numfiles
plot(all_data(K).dataAinT, all_data(K).dataEssT, 'DisplayName', all_data(K).name);
hold on
end
legend show
  5 个评论
Sam Vellequette
Sam Vellequette 2021-3-13
Hey, I did one quick last revision to attempt to condense it. Here's the final product. Thank you again!
dinfo = dir('TestRun*.xlsx');
numfiles = length(dinfo);
all_data(numfiles,1) = struct(); %preinitialize struct
for K = 1 : numfiles
filenames = {dinfo(K).name};
[~, basename, ~] = fileparts(char(filenames));
data = readmatrix(char(filenames));
all_data(K).dataAinT = data(:,1);
all_data(K).dataEssT = data(:,2);
all_data(K).name = basename;
end
for K = 1 : numfiles
plot(all_data(K).dataAinT, all_data(K).dataEssT, 'DisplayName', all_data(K).name);
hold on
end
legend show

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by