How to concatenate 5x2 array output from my function?

2 views (last 30 days)
Mitchell Barclay
Mitchell Barclay on 22 May 2022
Commented: Rik on 23 May 2022
I'm using a function to perform some data manipulation across 5 datasets. The data is cyclic voltammetry, so basically rectangles with x and y values. My function is outputting the values I want into a 5x2 array which works fine for single input. I want to use a for loop to have multiple inputs and have the output of the for loop in a single tall Yx2 array. currently, my 5x2 output is simply being overwritten every iteration.
How can I make sure that each iteration is stored in the same array so that it grows over the course of the for loop?
function Currents = Dunn(data1, data2, data3, data4, data5, potential, tolerance)
%Find the values near specified potential value. This step is needed since my data does not have 0.6, 0.61, 0.62...
%so I needed to use a net around the chosen value.
A = data1(:,1)>(potential -abs(potential)*tolerance) & (potential + abs(potential)*tolerance) > data1(:,1);
B = data2(:,1)>(potential -abs(potential)*tolerance) & (potential + abs(potential)*tolerance) > data2(:,1);
C = data3(:,1)>(potential -abs(potential)*tolerance) & (potential + abs(potential)*tolerance) > data3(:,1);
D = data4(:,1)>(potential -abs(potential)*tolerance) & (potential + abs(potential)*tolerance) > data4(:,1);
E = data5(:,1)>(potential -abs(potential)*tolerance) & (potential + abs(potential)*tolerance) > data5(:,1);
%initiate placeholder vectors that will later be concatenated.
placeholderA = [];
placeholderB = [];
placeholderC = [];
placeholderD = [];
placeholderE = [];
%initiate counters for the for loops
a = 1;
b = 1;
c = 1;
d = 1;
e = 1;
%Identify max y-axis value at specified 'potential' (x-axis) value for each dataset.
for i = 1:length(data1)
if A(i,1) > 0
placeholderA(a,1) = data1(i,1);
placeholderA(a,2) = data1(i,2);
a = a+1;
end
end
for i = 1:length(data2)
if B(i,1) > 0
placeholderB(b,1) = data2(i,1);
placeholderB(b,2) = data2(i,2);
b = b+1;
end
end
for i = 1:length(data3)
if C(i,1) > 0
placeholderC(c,1) = data3(i,1);
placeholderC(c,2) = data3(i,2);
c = c+1;
end
end
for i = 1:length(data4)
if D(i,1) > 0
placeholderD(d,1) = data4(i,1);
placeholderD(d,2) = data4(i,2);
d = d+1;
end
end
for i = 1:length(data5)
if E(i,1) > 0
placeholderE(e,1) = data5(i,1);
placeholderE(e,2) = data5(i,2);
e = e+1;
end
end
%concatenate the max y-value for each placeholder array into the output of the function as a 5x2 array.
Currents = [max(placeholderA);max(placeholderB);max(placeholderC);max(placeholderD);max(placeholderE)];
In my .m file I call the function with:
i = 0.5;
for n = 1:100
Currents = Dunn(mvs10, mvs30, mvs60, mvs100, mvs200, i, 0.01)
i = i +0.005
end

Accepted Answer

Rik
Rik on 22 May 2022
This should already get you most of the way there:
i = 0.5;
for n = 1:100
Currents{n} = Dunn(mvs10, mvs30, mvs60, mvs100, mvs200, i, 0.01);
i = i +0.005;
end
  2 Comments
Rik
Rik on 23 May 2022
Glad to be of help. If I solved the problem, please consider marking my answer as accepted answer. If you have any further questions, feel free to comment.

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!