Add values to a Matrix
2 views (last 30 days)
Show older comments
Hello!
I have the following code:
p=0;
num=0:1:3;
cont=0;
i=0;
b=0;
n=0;
for k=1:num(end)
cad = [];
cad = 'scope_%d_1.csv';
ss = [];
ss = sprintf(cad,num(k))
s = csvread(ss, 2, 0);
[fil,col]=size(s);
for i=1:fil
if ((s(i,2))< 0.5)&&(s(i+1,2)-(s(i,2))>2)&& p>0
cont=cont+1;
n=s(i, 1);
display(s(i, 1)-b);
p=p-1;
else if ((s(i,2))< 0.5)&&(s(i+1,2)-(s(i,2))>2)&& p==0
cont=cont+1;
display(s(i, 1)-n);
b=s(i, 1);
p=p+1;
else if (cont==25)
p=0;
num=0:1:3;
cont=0;
i=0;
b=0;
n=0;
break
end
end
end
end
end
And my intention is to, (in each iteration of any of both IF loops), save in a M matrix all the values that appears in the display (for all the iterations).
It is possible?
Thank you
2 Comments
Andrew Newell
on 25 Mar 2015
Hello, Antonio. When you have loops, it's much easier to understand the logic if you indent them, so I have done that for you. See Improve Code Readability.
Having done that, I still find it hard to understand your code or determine what values you want saving in a matrix. I suspect that, where you are writing else if, you really mean elseif. I also suggest that you look at your code in the MATLAB editor and fix all the problems that have an orange underline.
Answers (1)
Andrew Newell
on 26 Mar 2015
If you're trying to collect all the values for the inner loop ( for i=1:fil ), you could try putting
M = [];
above the loop, and replacing
display(s(i, 1)-b);
by
M = [M s(i, 1) - b];
and
display(s(i, 1)-n);
by
M = [M s(i, 1)-n];
It's better if a vector of the correct size is preallocated before the loop starts, but I don't know how large M will be.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!