MATLAB Answers

How to add value to tables (inside a cell) from the other array?

2 views (last 30 days)
Dear all
I have an array 1x4 (namely point) and a cell 1x4 (namely C). In the C I have 6x1 tables. I want to respectively copy the value of point as the second column of each table (first cell of point for the first array of C, second cell of point for the second array of C, and so on... In fact, each array of points repeat 6 times and paste into cell arrays.
I would be grateful if anyone can tell me how it is possible.
Thank you

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 10 May 2020
This will do it:
% Load 2 mat files:
s = load('c.mat')
c = s.C
s = load('point.mat')
p = s.point
% Now stitch p, as a column vector, onto
% the right side of each table in each cell.
for column = 1 : length(c)
% Get this table by extracting it from this cell of the cell array.
t = c{column}
% Make another column of 6 rows onto t.
% All 6 values in the second column will be 0 to start with.
t{end, 2} = 0;
% Fill up the first 4 rows of the second column
% of this table with p. Last 2 rows will remain 0.
t{1:length(p), 2} = p(:)
% Stuff new table back into the cell
c{column} = t;
end

  3 Comments

Behzad Navidi
Behzad Navidi on 11 May 2020
Dear Image Analyst,
Thank you. I'm sorry if I was not clear; I needed to repeat the first cell of point, 6 times, and then add it to the right side of the first table; repeat the second cell of point, 6 times, and then add it to the right side of the second table and so on. I need something like this (I build it manually)
The output for C{1, 1}:
The output for C{1, 2}:
The output for C{1, 3}:
And so on. So I change the code to:
for column = 1 : length(c)
% Get this table by extracting it from this cell of the cell array.
t = c{column};
% Make another column of 6 rows onto t.
% All 6 values in the second column will be 0 to start with.
% t{end, 2} = 0;
% Fill up the first 4 rows of the second column
% of this table with p. Last 2 rows will remain 0.
t{1:length(p), 2} = p(column);
% Stuff new table back into the cell
c{column} = t;
end
But I wonder why 2 last rows stay zero:
and also this warning appear:
Warning: The new variables being added to the table have fewer rows than
the table. They have been extended with rows containing default values.
> In tabular/subsasgnParens (line 384)
In tabular/subsasgnBraces (line 156)
In tabular/subsasgn (line 67)
Warning: The new variables being added to the table have fewer rows than
the table. They have been extended with rows containing default values.
> In tabular/subsasgnParens (line 384)
In tabular/subsasgnBraces (line 156)
In tabular/subsasgn (line 67)
Warning: The new variables being added to the table have fewer rows than
the table. They have been extended with rows containing default values.
> In tabular/subsasgnParens (line 384)
In tabular/subsasgnBraces (line 156)
In tabular/subsasgn (line 67)
Warning: The new variables being added to the table have fewer rows than
the table. They have been extended with rows containing default values.
> In tabular/subsasgnParens (line 384)
In tabular/subsasgnBraces (line 156)
In tabular/subsasgn (line 67)
Thank you again.
Behzad Navidi
Behzad Navidi on 11 May 2020
Thank you I use this:
p=repmat(p,6,1);
for i = 1:length(c)
t = c{i};
t{1:height(c{1, 1}), 2} = p(:,i);
c{i} = t;
end
And it fixed.
Thank you so much
Image Analyst
Image Analyst on 11 May 2020
Try it this way:
% Load 2 mat files:
s = load('c.mat')
c = s.C
s = load('point.mat')
p = s.point
% Now stitch p, as a column vector, onto
% the right side of each table in each cell.
for column = 1 : length(c)
% Get this table by extracting it from this cell of the cell array.
t = c{column};
% Make another column of 6 rows onto t.
% All 6 values in the second column will be 0 to start with.
t{end, 2} = 0;
% Fill up the first 4 rows of the second column
% of this table with p. Last 2 rows will remain 0.
t{:, 2} = p(column);
% Stuff new table back into the cell
c{column} = t;
end

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Translated by