MATLAB Answers

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

4 views (last 30 days)
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.
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 :
data = importdata('Example file MatLab.xlsx');
data =;
xx = 1:length(data);
dy = gradient(data,1);
ind = find(abs(dy)<0.5e-5); % results may vary according to slope threshold
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);
% if k>=1
out2{ck} = out;
out = [];
k = 1;
ck = ck+1;
% 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(indAA==0) = [];
indBB(indBB==0) = [];
indCC(indCC==0) = [];
indDD(indDD==0) = [];
legend('data','A sections','B sections','C sections','D sections');

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!