1

I'm trying to replace the 5th column in each cell in a cell array with the 5th column of each cell from another cell array. I made the following function, which does this but also replaces the values in all other columns with 0. How do I do this without deleting all other values from the other columns. The function is:

function [X]=replace_cells(cell) 
X={};
for i=1:length(cell)
   X{i}(:,[5])=cell{i}(:,[5]);
end

end

1
  • 1
    X has been initialized as an empty array. You are only copying the 5th column from each cell of cell into X so the other values can only be 0. I would also highly recommend not using cell as a variable name. Commented Feb 23, 2016 at 12:16

1 Answer 1

1

Your function doesn't replace the columns because the function creates X while it should be an input, try this function,

function X = replace_cells(c,X) 
for i = 1 : length(c)
   X{i}(:,5)=c{i}(:,5);
end

cell is a Matlab function don't use it as name for variables.

Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't do what you say it does. Arguments (if they are not handle objects) are copied into the function's workspace, which means that your function will alter a copy, not the original. If you want to fix it you should declare your function like this function X=replace_cells(c,X).
There is probably a return argument missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.