How can I create a function of my code

조회 수: 1 (최근 30일)
flashpode
flashpode 2022년 2월 3일
댓글: flashpode 2022년 2월 5일
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

채택된 답변

DGM
DGM 2022년 2월 3일
편집: DGM 2022년 2월 3일
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
DGM
DGM 2022년 2월 3일
I don't understand what you're asking. Fom the perspective of the calling scope, the variable passed to the function is not changed.
flashpode
flashpode 2022년 2월 5일
great!! Than you!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by