Automatically select and name variables from import file (.xlsx)
2 次查看(过去 30 天)
显示 更早的评论
Hello,
I have an excel file with a column of variables. It looks like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/613825/image.png)
The sequence is approx. 0.0237, -0.0016, 0.0018, 0.0239. Which should be named A, B, C, D. This sequence repeats in the same order 50 times (A1,B1,C1,D1, A2, B2, C2, D2, etc), but the values are different each time and the number of cells for A1, B1, etc varies. A and D are always in the range of 0.0001 to 0.0320, while B and C are always in the range 0.0010 to 0.0020.
At the moment, I am highlighting the cells from Import Data and naming them one by one.
I would like to automatically select 10 cells before B1, and name those A1. Automatically select all the cells for B1 and name them B1. Automatically select all the cells for C1 and name them C1. Finally, automatically select 10 cells after C1 and name them D1. This would be repeated for the other 49 times.
Any help would be appreciated!
Thank you.
5 个评论
采纳的回答
Mathieu NOE
2021-5-12
ok so that's my first trial , based on slope values (must be below a given threshold to select sections of interest)
not sure I got all 10 points as per request , but so far i am not sure how to improve my code
results :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/616010/image.png)
code :
clc
clearvars
data = importdata('Example file MatLab.xlsx');
data = data.data;
xx = 1:length(data);
dy = gradient(data,1);
ind = find(abs(dy)<0.5e-5); % results may vary according to slope threshold
figure(1);
subplot(211),plot(xx,data,'b',xx(ind),data(ind),'r+');
subplot(212),plot(dy);
ck = 1;
k = 1;
out = [];
out(1) = ind(1);
for ci = 2:length(ind)
ind_delta = ind(ci)-ind(ci-1);
if (ind_delta-1)<eps
k = k+1;
out(k,1) = ind(ci);
else
% if k>=1
out2{ck} = out;
out = [];
k = 1;
ck = ck+1;
% end
end
end
% last buffer
out2{ck} = out;
% expected result is 40 arrays (out2)
nb_arrays = numel(out2);
indA = (out2((1:4:nb_arrays)))';
indB = (out2((2:4:nb_arrays)))';
indC = (out2((3:4:nb_arrays)))';
indD = (out2((4:4:nb_arrays)))';
indAA=cell2mat(indA);
indAA(indAA==0) = [];
indBB=cell2mat(indB);
indBB(indBB==0) = [];
indCC=cell2mat(indC);
indCC(indCC==0) = [];
indDD=cell2mat(indD);
indDD(indDD==0) = [];
figure(2);
plot(xx,data,'b',xx(indAA),data(indAA),'r+',...
xx(indBB),data(indBB),'g+',...
xx(indCC),data(indCC),'m+',...
xx(indDD),data(indDD),'c+');
legend('data','A sections','B sections','C sections','D sections');
4 个评论
更多回答(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!