Matlab coder "Error indenting generated C code" - matlab
I am Trying to convert a MATLAB code to C++ using MATLAB coder but this error apears:
Error indenting generated C code
The error points to the name of the function itself and has no more explanations in it. can someone tell me what is this error?
here is the function i want to conver:
function [Report_Clustered,ClusterCounter_new]=InitClusterGenerator_test(Report_In,~,FreqEpsilon,DegreeEpsilon,~,ClusterCounter_old, BlockCount, Report_old)
Report_M = zeros(size(Report_In,1),size(Report_In,2),4);
for i=1:size(Report_In,1)
for j=1:size(Report_In,2)
Report_M(i,j,1)=Report_In(i,j,1);
Report_M(i,j,2)=Report_In(i,j,2);
Report_M(i,j,3)=0; % Cluster number that the point belongs to.
Report_M(i,j,4)=0;
Report_In{i,j}
end
end
ClusterCounter = 0;
for i=1:size(Report_M,1)
for j=1:size(Report_M,2)
if (Report_M(i,j,3) == 0)
ClusterCounter = ClusterCounter + 1;
Report_M(i,j,3) = ClusterCounter;
for ii=1:size(Report_M,1)
for jj=1:size(Report_M,2)
if (Report_M(ii,jj,3) == 0)
if (abs(Report_M(i,j,1)-Report_M(ii,jj,1))<FreqEpsilon &&...
(abs(Report_M(i,j,2)-Report_M(ii,jj,2)) <DegreeEpsilon ||...
abs(-360 + Report_M(i,j,2)-Report_M(ii,jj,2)) <DegreeEpsilon ||...
abs(360 + Report_M(i,j,2)-Report_M(ii,jj,2)) <DegreeEpsilon))
Report_M(ii,jj,3) = ClusterCounter;
end
end
end
end
end
end
end
if (BlockCount> 20 && ClusterCounter<4)
warning = 1;
end
ClusterCounter_new = ClusterCounter;
%clear Report_new;
flag = 0;
Report_new = zeros(ClusterCounter,size (Report_M, 2),4);
index = zeros(1, ClusterCounter_new);
for i = 1: size (Report_M, 1)
for j = 1: size (Report_M, 2)
for k = 1: ClusterCounter_new
if (Report_M(i,j,3) == k)
index(1,k) = index(1,k) + 1;
Report_new(k,index(1,k), 1:3) = Report_M(i,j,1:3);
flag = flag + 1;
end
end
end
end
for j = 1: size (Report_new, 2)
for i = 1: size (Report_new, 1)
if (Report_new(i,j,1) == 0)
Report_new(i,j,1:3) = Report_new(i,1,1:3);
end
end
end
%Report_new = Report;
MedoidF_old = zeros(1, size(Report_old,1));
MedoidA_old = zeros(1, size(Report_old,1));
for i=1:size(Report_old,1)
SumF = 0;
SumA = 0;
MinAngle = 361;
MaxAngle = -1;
for j=1:size(Report_old,2)
SumF = SumF + Report_old(i,j,1);
SumA = SumA + Report_old(i,j,2);
if Report_old(i,j,2) > MaxAngle
MaxAngle = Report_old(i,j,2);
elseif Report_old(i,j,2) < MinAngle
MinAngle = Report_old(i,j,2);
end
end
MedoidF_old(1, i) = SumF/size(Report_old,2);
if (MaxAngle - MinAngle) > 350
MedoidA_old(1, i) = 0;
else
MedoidA_old(1, i) = SumA/size(Report_old,2);
end
end
MedoidF_new = zeros(1, size(Report_new,1));
MedoidA_new = zeros(1, size(Report_new,1));
for i=1:size(Report_new,1)
SumF = 0;
SumA = 0;
MinAngle = 361;
MaxAngle = -1;
for j=1:size(Report_new,2)
SumF = SumF + Report_new(i,j,1);
SumA = SumA + Report_new(i,j,2);
if Report_new(i,j,2) > MaxAngle
MaxAngle = Report_new(i,j,2);
elseif Report_new(i,j,2) < MinAngle
MinAngle = Report_new(i,j,2);
end
end
MedoidF_new(1, i) = SumF/size(Report_new,2);
if (MaxAngle - MinAngle) > 350
MedoidA_new(1, i) = 0;
else
MedoidA_new(1, i) = SumA/size(Report_new,2);
end
end
TempCluster = zeros(1, size(Report_new, 1));
CurrentCluster = ClusterCounter_old;
for i = 1: 1: size(Report_new,1)
for j = 1: 1: size(Report_old,1)
if (abs(MedoidF_old(1,j)-MedoidF_new(1,i))<FreqEpsilon &&...
(abs(MedoidA_old(1,j)-MedoidA_new(1,i))<DegreeEpsilon ||...
abs(360 + MedoidA_old(1,j)-MedoidA_new(1,i))<DegreeEpsilon ||...
abs(-360 + MedoidA_old(1,j)-MedoidA_new(1,i))<DegreeEpsilon)) %%if the new cluster is the rest of an old cluster use the old one's index for it
TempCluster(1,i) = Report_old(j,1,3);
end
end
%%this part is for seperating the clusters which where in the collision state in the past time
if (TempCluster(1,i)>0) %%if the new cluster is one of the old ones the index should be set
for j = 1:1:size(Report_new, 2)
Report_new(i,j,3) = TempCluster(1,i);
Report_new(i,j,4) = 1;% Alive
end
else %%first search if the new cluster is a part of a newly found cluster found before this one
for j = 1: 1: i-1
if (abs(MedoidF_new(1,j)-MedoidF_new(1,i))<FreqEpsilon &&...
(abs(MedoidA_new(1,j)-MedoidA_new(1,i))<DegreeEpsilon ||...
abs(360 + MedoidA_new(1,j)-MedoidA_new(1,i))<DegreeEpsilon ||...
abs(-360 + MedoidA_new(1,j)-MedoidA_new(1,i))<DegreeEpsilon)) %%if the new cluster is the rest of an old cluster use the old one's index for it
TempCluster(1,i) = Report_new(j,1,3);
end
end
end
if (TempCluster(1,i)>0) %%if the new cluster is one of the old ones the index should be set
for j = 1:1:size(Report_new, 2)
Report_new(i,j,3) = TempCluster(1,i);
Report_new(i,j,4) = 1;% Alive
end
else %%new cluster is just began so it needs a new index
CurrentCluster = CurrentCluster + 1;
ClusterCounter_new = CurrentCluster;
TempCluster(1,i) = CurrentCluster;
for j = 1:1:size(Report_new, 2)
Report_new(i,j,3) = TempCluster(1,i);
Report_new(i,j,4) = 1; % Alive
end
end
end
NewClusters = zeros(1, size (Report_new, 1));
for i = 1: size(Report_new, 1)
NewClusters (1,i) = Report_new(i,1,3);
end
OldClusters = zeros(1, size (Report_old, 1));
OldClustersLine = zeros(1, size (Report_old, 1));
for i = 1: size(Report_old, 1)
OldClusters (1,i) = Report_old(i,1,3);
OldClustersLine (1, i) = i;
end
NumberOfDead = 0;
%clear AddDead;
AddDead = zeros (16,size(Report_new, 2),4);
if (BlockCount>10)
for i = 1: size (OldClusters, 2)
IsDead = 1;
for j = 1: size (NewClusters, 2)
if OldClusters(1, i) == NewClusters(1,j)
IsDead = 0;
end
end
if (IsDead == 1)
NumberOfDead = NumberOfDead + 1;
%clear TempLine;
TempLine = zeros(1, size(Report_old,2), 4);
TempLine(1,:,1:3) = Report_old(OldClustersLine(1, i),:,1:3);
for k= 1: size(TempLine, 2)
TempLine(1,k,4) = 0; % Dead
end
TempSize = size(TempLine, 2);
Thresh = size(Report_new, 2);
if (TempSize >= Thresh)
AddDead (NumberOfDead, 1:Thresh, 1:4) = TempLine(1,1:Thresh, 1:4);
else
for l = 1: Thresh-TempSize
TempLine(1, TempSize+l, 1:4) = TempLine(1, TempSize, 1:4);
end
AddDead (NumberOfDead, 1:Thresh, 1:4) = TempLine(1,1:Thresh, 1:4);
end
end
end
xR = size (Report_new,1);
if (NumberOfDead == 0)
Report_Clustered = zeros (size(Report_new,1),size(Report_new,2),size(Report_new,3));
else
Report_Clustered = zeros (size(Report_new,1) + NumberOfDead,size(Report_new,2),size(Report_new,3));
end
Report_Clustered (1:size(Report_new,1), :, :) = Report_new(:,:,:);
for i = 1: NumberOfDead
Report_Clustered(xR + i, :) = AddDead(i, :);
end
end
and I'm using matlab 2012a
Tnx.
From what you've said in the comments, it appears that you simply need to call
clear functions
from the command line before recompiling the function to allow Matlab to overwrite the files. See this Matlab forum or the documentation for clear for more detail.
Related
MATLAB 'parfor' Loops Very Slow When Compared With 'for' loop
I have a script that I'm running, and at one point I have a loop over n objects, where I want n to be fairly large. I have access to a server, so I put in a parfor loop. However, this is incredibly slow compared with a standard for loops. For example, running a certain configuration ( the one below ) with the parfor loop on 35 workers took 68 seconds, whereas the for loop took 2.3 seconds. I know there's stuff to do with array-broadcasting that can cause issues, but I don't know a lot about this. n = 20; r = 1/30; tic X = rand([2,n-1]); X = [X,[0.5;0.5]]; D = sq_distance(X,X); A = sparse((D < r) - eye(n)); % Infected set I I = n; [S,C] = graphconncomp(A); compnum = C(I); I_new = find(C == compnum); I = I_new; figure%('visible','off') gplot(A,X') hold on plot(X(1,I),X(2,I),'r.') hold off title('time = 0') axis([0,1,0,1]) time = 0; t_max = 10; t_int = 1/100; TIME = 1; T_plot = t_int^(-1) /100; loops = t_max / T_plot; F(loops) = struct('cdata',[],'colormap',[]); F(1) = getframe; % Probability of healing in interval of length t_int heal_rate = 1/3; % (higher number is faster heal) p_heal = t_int * heal_rate; numhealed = 0; while time < t_max time = time+t_int; steps = poissrnd(t_int,[n,1]); parfor k = 1:n for s = 1:steps(k) unit_vec = unif_unitvector; X_new = X(:,k) + unit_vec*t_int; if ( X_new < 1 == ones(2,1) ) ... & ( X_new > 0 == ones(2,1) ) X(:,k) = X_new; end end end D = sq_distance(X,X); A = sparse((D < r) - eye(n)); [S,C] = graphconncomp(A); particles_healed = binornd(ones(length(I),1),p_heal); still_infected = find(particles_healed == 0); I = I(still_infected); numhealed = numhealed + sum(particles_healed); I_new = I; % compnum = zeros(length(I),1); for i = 1:length(I) compnum = C(I(i)); I_new = union(I_new,find(C == compnum)); end I = I_new; if time >= T_plot*TIME gplot(A,X') hold on plot(X(1,I),X(2,I),'r.') hold off title(sprintf('time = %1g',time)) axis([0,1,0,1]) % fprintf('number healed = %1g\n',numhealed) numhealed = 0; F(TIME) = getframe; TIME = TIME + 1; end end toc
MATLAB 3D lines are invisible
Hello I'am suffered from a problem As you can see I want a draw 3D graph. Problem is when I draw sphere lines are invisible. Here is simple version of my source clear all; close all; clc n=1; n_inner_drone=3; n_outter_drone=2; length=100; initial_d = zeros(1,n); inner_x = zeros(n_inner_drone,n); inner_y = zeros(n_inner_drone,n); inner_z = zeros(n_inner_drone,n); outter_x = zeros(n_outter_drone,n); outter_y = zeros(n_outter_drone,n); outter_z = zeros(n_outter_drone,n); radius= length; disp('test'); %%%%%%%%%%%%%%%%%%%%%% Sphere % figure() % [x, y, z] = sphere; % h = surfl(x*length, y*length, z*length); % hSurf = surf(X,Y,Z,'EdgeColor','none','LineStyle','none','FaceLighting','phong'); % set(h, 'FaceAlpha', 0.05) % surf(x*length, y*length, z*length, % shading interp hold on %%%%%%%%%%%%%%%%%%%%%%%%% for i=1:n_inner_drone k=1; while 1 x_temp= randi([-length, length], 1, 1); y_temp= randi([-length, length], 1, 1); z_temp= randi([-length, length], 1, 1); dist = sqrt(x_temp^2 + y_temp^2 + z_temp^2); if dist<radius if i==1 initial_d(k) = dist; end inner_x(i,k) = x_temp; inner_y(i,k) = y_temp; inner_z(i,k) = z_temp; k = k+1; end if k == n+1, break, end end end ideal_direction_length = ones(1,n); ideal_direction_length = ideal_direction_length * length; ideal_direction_length = ideal_direction_length - initial_d; k=1; random_x = inner_x(1,:); random_y = inner_y(1,:); random_z = inner_z(1,:); random_moving_distance = zeros(1,n); moving_distance = 0; trigger = 0; while 1 if trigger == 0 direction = randi([1, 6], 1, 1); trigger = 1; end if direction == 1 random_x(k) = random_x(k) + 1; elseif direction == 2 random_x(k) = random_x(k) - 1; elseif direction == 3 random_y(k) = random_y(k) + 1; elseif direction == 4 random_y(k) = random_y(k) - 1; elseif direction == 5 random_z(k) = random_z(k) + 1; elseif direction == 6 random_z(k) = random_z(k) - 1; end dist = sqrt(random_x(k)^2 + random_y(k)^2 + random_z(k)^2); moving_distance = moving_distance+1; %%%%%%%%%% Line plot3(random_x(n),random_y(n),random_z(n),'k+') %%%%%%%%%%%%%%% if dist>radius random_moving_distance(k) = moving_distance; k = k+1; moving_distance = 0; trigger = 0; end if k == n+1, break, end end plot3(inner_x(1,n),inner_y(1,n),inner_z(1,n),'r*') for k=2:n_inner_drone plot3(inner_x(k,n),inner_y(k,n),inner_z(k,n),'b*') end for k=1:n_outter_drone plot3(outter_x(k,n),outter_y(k,n),outter_z(k,n),'k*') end At the first, I suspected I worngly draw lines, but without sphere I can see lines as fig2. Those anyone who knows about this problem. Please answer to me and I will very appericiate about it. Thanks for reading.
I think it is because: plot3(gravity_x(n),gravity_y(n),gravity_z(n)) is not a line. Its a single point. plot3(gravity_x(n:n+1),gravity_y(n:n+1),gravity_z(n:n+1)) is a line.
how to Improve the speed matlab
This is my matlab code. It runs too slow and I had no clue how to improve it. Could you help me to improve the speed? What I would like to do is to create some random points and then remove the random points to make them similar to my target points. syms Dx Dy p q; a = 0; num = 10; x = rand(1,num); y = rand(1,num); figure(1) scatter(x,y,'.','g') %num_x = xlsread('F:\bin\test_2');% num 1024 %figure(2) %scatter(num_x(:,1),num_x(:,2),'.','r'); q = 0; num_q = 10; x_q = randn(1,num_q); y_q = randn(1,num_q); %figure(2) hold on; scatter(x_q,y_q,'.','r') for i = 1:num_q; for j = 1:num_q; qx(i,j) = x_q(i) - x_q(j); qy(i,j) = y_q(i) - y_q(j); %qx(i,j) = num_x(i,1) - num_x(j,1); %qy(i,j) = num_x(i,2) - num_x(j,2); %d~(s(i),s(j)) if ((qx(i,j))^2+(qy(i,j)^2))> 0.01 % find neighbours qx(i,j) = 0; qy(i,j) = 0; end end end for i = 1:num_q; for j = 1:num_q; if qx(i,j)>0&&qy(i,j)>0 q = q + exp(-(((Dx - qx(i,j))^2)+((Dy - qy(i,j))^2))/4);%exp(-(((Dx - qx(i,j))^2)+((Dy - qy(i,j))^2))/4); end end end %I = ones(num,num); % I(s) should from a grayscale image %r = 1./sqrt(I); for s = 1:100; for i = 1:num; for j = 1:num; dx(i,j) = x(i) - x(j); dy(i,j) = y(i) - y(j); %d~(s(i),s(j)) if ((dx(i,j))^2+(dy(i,j)^2))> 0.05 % delta p, find neighbours dx(i,j) = 0; dy(i,j) = 0; end end end p = 0; for i = 1:num; for j = 1:num; if dx(i,j)>0&&dy(i,j)>0 p = p + exp(-(((Dx - dx(i,j))^2)+((Dy - dy(i,j))^2))/4); end end end p = p - q; sum = 0; for i = 1:num; for j = 1:num; if dx(i,j)>0&&dy(i,j)>0; kx(i,j) = (1/2)*(Dx-dx(i,j))*exp((-(Dx-dx(i,j))^2+(Dy-dy(i,j))^2)/4); ky(i,j) = (1/2)*(Dy-dy(i,j))*exp((-(Dx-dx(i,j))^2+(Dy-dy(i,j))^2)/4); end end end sum_x = ones(1,num);% 1行N列0矩阵 sum_y = ones(1,num); %fx = zeros(1,num); for i = 1:num; for j = 1:num; if dx(i,j)>0&&dy(i,j)>0; fx(i) = p*kx(i,j);% j is neighbour to i fy(i) = p*ky(i,j); %fx(i) = matlabFunction(fx(i)); %fy(i) = matlabFunction(fy(i)); %P =quad2d(#(Dx,Dy) fx,0,0.01,0,0.01); %fx =quad(#(Dx) fx,0,0.01); %fx(i) =quad(#(Dy) fx(i),0,0.01); %Q =quad2d(#(Dx,Dy) fy,0,0.01,0,0.01); fx(i) = double(int(int(fx(i),Dx,0,0.01),Dy,0,0.01)); fy(i) = double(int(int(fy(i),Dx,0,0.01),Dy,0,0.01)); %fx(i) = vpa(p*kx(i,j)); %fy(i) = vpa(p*ky(i,j)); %fx(i) = dblquad(#(Dx,Dy)fx(i),0,0.01,0,0.01); %fy(i) = dblquad(#(Dx,Dy)fy(i),0,0.01,0,0.01); sum_x(i) = sum_x(i) + fx(i); sum_y(i) = sum_y(i) + fy(i); end end end for i = 1:num; sum_x = 4.*sum_x./num; sum_y = 4.*sum_y./num; x(i) = x(i) - 0.05*sum_x(i); y(i) = y(i) - 0.05*sum_y(i); end a = a+1 end hold on; scatter(x,y,'.','b')
The fast version of your loop should be something like: qx = bsxfun(#minus, x_q.', x_q); qy = bsxfun(#minus, y_q.', y_q); il = (qx.^2 + qy.^2 >= 0.01); qx(il) = 0; qy(il) = 0; il = qx>0 && qy>0; q = sum(exp(-((Dx-qx(il)).^2 + (Dy-qy(il)).^2)/4)); %// etc. for vectorization of the inner loops
Filter points using hist in matlab
I have a vector. I want to remove outliers. I got bin and no of values in that bin. I want to remove all points based on the number of elements in each bin. Data: d1 =[ 360.471912914169 505.084636471948 514.39429429184 505.285068055647 536.321181755858 503.025854206322 534.304229816684 393.387035881967 396.497969729985 520.592172434431 421.284713703215 420.401106087984 537.05330275495 396.715779872694 514.39429429184 404.442344469518 476.846474245118 599.020867750031 429.163139144079 514.941744277933 445.426761656729 531.013596812737 374.977332648255 364.660115724218 538.306752697753 519.042387479096 1412.54699036882 405.571202133485 516.606049132218 2289.49623498271 378.228766753667 504.730621222846 358.715764917016 462.339366699398 512.429858614816 394.778786157514 366 498.760463549388 366.552861126468 355.37022947906 358.308526273099 376.745272034036 366.934599077274 536.0901883079 483.01740134285 508.975480745389 365.629593988233 536.368800360349 557.024236456548 366.776498701866 501.007025898839 330.686029339009 508.395475983019 429.563732174866 2224.68806802212 534.655786464525 518.711297351426 534.304229816684 514.941744277933 420.32368479542 367.129404978681 525.626188464768 388.329756778952 1251.30895065927 525.626188464768 412.313764019587 513.697381733643 506.675438520558 1517.71183364959 550.276294237722 543.359917550053 500.639590923451 395.129864728041]; Histogram computation: [nelements,centers] = hist(d1); nelements=55 13 0 0 1 1 1 0 0 2 I want to remove all points apearing less than 5 (in nelements). It means only first 2 elements in nelements( 55, 13 ) remains. Is there any function in matlab.
You can do it along these lines: threshold = 5; bin_halfwidth = (centers(2)-centers(1))/2; keep = ~any(abs(bsxfun(#minus, d1, centers(nelements<threshold))) < bin_halfwidth , 2); d1_keep = d1(keep);
Does this do what you want? binwidth = centers(2)-centers(1); centersOfRemainingBins = centers(nelements>5); remainingvals = false(length(d1),1); for ii = 1:length(centersOfRemainingBins ) remainingvals = remainingvals | (d1>centersOfRemainingBins (ii)-binwidth/2 & d1<centersOfRemainingBins (ii)+binwidth/2); end d_out = d1(remainingvals);
I don't know Matlab function for this problem, but I think, that function with follow code is what are you looking for: sizeData = size(data); function filter_hist = filter_hist(data, binCountRemove) if or(max(sizeData) == 0, binCountRemove < 1) disp('Error input!'); filter_hist = []; return; end [n, c] = hist(data); sizeN = size(n); intervalSize = c(2) - c(1); if sizeData(1) > sizeData(2) temp = transpose(data); else temp = data; end for i = 1:1:max(sizeN) if n(i) < binCountRemove a = c(i) - intervalSize / 2; b = c(i) + intervalSize / 2; sizeTemp = size(temp); removeInds = []; k = 0; for j = 1:1:max(sizeTemp) if and(temp(j) > a, less_equal(temp(j), b) == 1) k = k + 1; removeInds(k) = j; end end temp(removeInds) = []; end end filter_hist = transpose(temp); %Determines when 'a' less or equal to 'b' by accuracy function less_equal = less_equal(a, b) delta = 10^-6; %Accuracy if a < b less_equal = 1; return; end if abs(b - a) < delta less_equal = 1; return; end less_equal = 0;
You can do something like this nelements=nelements((nelements >5))
incapsulation of a code inmatlab
my code is pathname=uigetdir; filename=uigetfile('*.txt','choose a file name.'); data=importdata(filename); element= (data.data(:,10)); in_array=element; pattern= [1 3]; locations = cell(1, numel(pattern)); for p = 1:(numel(pattern)) locations{p} = find(in_array == pattern(p)); end idx2 = []; for p = 1:numel(locations{1}) start_value = locations{1}(p); for q = 2:numel(locations) found = true; if (~any((start_value + q - 1) == locations{q})) found = false; break; end end if (found) idx2(end + 1) = locations{1}(p); end end [m2,n2]=size(idx2) res_name= {'one' 'two'}; res=[n n2]; In this code I finding a pattern in one of the column of my data file and counting how many times it's repeated. I have like 200 files that I want to do the same with them but unfotunatlly I'm stuck. this is what I have added so far pathname=uigetdir; files=dir('*.txt'); for k=1:length(files) filename=files(k).name; data(k)=importdata(files(k).name); element{k}=data(1,k).data(:,20); in_array=element;pattern= [1 3]; locations = cell(1, numel(pattern)); for p = 1:(numel(pattern)) locations{p} = find(in_array{k}== pattern(p)); end idx2{k} = []; how can I continue this code..??
OK, first define this function: function [inds, indsy] = findPattern(M, pat, dim) indices = []; if nargin == 2 dim = 1; if size(M,1) == 1 dim = 2; end end if dim == 1 if numel(pat) > size(M,1) return; end for ii = 1:size(M,2) inds = findPatternCol(M(:,ii), pat); indices = [indices; repmat(ii,numel(inds),1) inds]%#ok end elseif dim == 2 if numel(pat) > size(M,2) return; end for ii = 1:size(M,1) inds = findPatternCol(M(ii,:).', pat); indices = [indices; inds repmat(ii,numel(inds),1)]%#ok end else end inds = indices; if nargout > 1 inds = indices(:,1); indsy = indices(:,2); end end function indices = findPatternCol(col, pat) inds = find(col == pat(1)); ii = 1; prevInds = []; while ~isempty(inds) && ii<numel(pat) && numel(prevInds)~=numel(inds) prevInds = inds; inds = inds(inds+ii<=numel(col) & col(inds+ii)==pat(ii+1)); ii = ii + 1; end indices = inds(:); end which is decent but probably not the most efficient. If performance becomes a problem, start here with optimizations. Now loop through each file like so: pathname = uigetdir; files = dir('*.txt'); indices = cell(length(files), 1); for k = 1:length(files) filename = files(k).name; data(k) = importdata(files(k).name); array = data(1,k).data(:,20); pattern = [1 3]; indices{k} = findPattern(array, pattern); end The number of occurrences of the pattern can be found like so: counts = cellfun(#(x)size(x,1), indices);