MATLAB Answers

Automatically select and name variables from import file (.xlsx)

4 views (last 30 days)
Hello,
I have an excel file with a column of variables. It looks like this:
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 Comments
meryem berrada
meryem berrada on 12 May 2021
Thank you for your answers. If it helps, we can make B always the negative sequence (so B and C would be interchangeable).
Would there be a way to:
  • find the first deviation from the mean, name it B1
  • then, select the 10 values before B1, name it A1
  • select all the values between B1 and the next change, name it C1
  • select 10 values after C1 and name it D1
Or, is there a way to do highlighting the cells manually in the Import data folder and press enter (for example), and the selected values would automatically be named A1 ? Then I can highlight the next set of values and press enter and it would automatically be named B1, etc.
Thank you,

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 12 May 2021
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 :
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 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!