How can I create a function of my code

1 view (last 30 days)
Hi, I got a code that I want to convert to a a function so I do not write for each variable the whole code. I do not really understand the documentation page because I have a code that is quite big and I get confused. My code is to generate the same variable without the doubles inside. The output that I want is the same name as the variable input. So that is where I got the problem. I post the code so you can see:
function y = removedbl(var)
numCells = numel(Time_msg_match); %Time_msg_match was the original variable for this code was created
Time_msg_match2 = zeros(numCells+10000, 1);
vector2Index = 1;
for k = 1 : numCells
len = length(Time_msg_match{k});
if len == 1
Time_msg_match2(vector2Index) = Time_msg_match{k};
vector2Index = vector2Index + 1;
else
fprintf('Row %d has %d elements in it.\n', k, length(Time_msg_match{k}));
for k2 = 1 : len
thisVector = Time_msg_match{k};
Time_msg_match2(vector2Index) = thisVector(k2);
vector2Index = vector2Index + 1;
end
end
end
if vector2Index < numCells
Time_msg_match2 = Time_msg_match2(1 : vector2Index - 1);
end
% fprintf('Original Time_msg_match had %d rows.\n', numCells) %Nos dice de que numero a que numero pasamos al quitar las celdas vacias
% fprintf('Afterwards Time_msg_match had %d rows.\n', numel(Time_msg_match2))
end

Accepted Answer

DGM
DGM on 3 Feb 2022
Edited: DGM on 3 Feb 2022
This probably covers what you are trying to do. I assume your problem description is as follows:
  1. You have a cell array of scalars and row vectors.
  2. Read the cells in column-major order
  3. Reshape the cell contents to a column vector
  4. Concatenate to form one large column vector
aaa = {1,2,3,4,5,ones(1,5),2*ones(1,5),3*ones(1,5)};
bbb = removedbl(aaa)
Row 6 has 5 elements in it. Row 7 has 5 elements in it. Row 8 has 5 elements in it.
bbb = 20×1
1 2 3 4 5 1 1 1 1 1
ccc = vectorizecell(aaa)
ccc = 20×1
1 2 3 4 5 1 1 1 1 1
% this is your function with some fixes
function Time_msg_match2 = removedbl(Time_msg_match)
numCells = numel(Time_msg_match); %Time_msg_match was the original variable for this code was created
Time_msg_match2 = zeros(numCells+10000, 1);
vector2Index = 1;
for k = 1 : numCells
len = length(Time_msg_match{k});
if len == 1
Time_msg_match2(vector2Index) = Time_msg_match{k};
vector2Index = vector2Index + 1;
else
fprintf('Row %d has %d elements in it.\n', k, length(Time_msg_match{k}));
for k2 = 1 : len
thisVector = Time_msg_match{k};
Time_msg_match2(vector2Index) = thisVector(k2);
vector2Index = vector2Index + 1;
end
end
end
if vector2Index < numCells+10000 % to fix padding
Time_msg_match2 = Time_msg_match2(1 : vector2Index - 1);
end
end
% this is an alternative method to do the same
function outvec = vectorizecell(inarray)
isrv = cellfun(@isrow,inarray);
if ~all(isrv(:))
error('VECTORIZECELL: some cells do not contain row vectors')
end
inarray = cellfun(@(x) x(:),inarray,'uniform',false);
outvec = cell2mat(inarray(:));
end
If the restriction to "row vectors only" does not apply, you can remove the first few lines of vectorizecell(). Matrix contents will be vectorized in column-major order. If you want it to handle these cases in row-major order, then let me know.
  3 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!