Reputation: 629
I have a MATLAB matrix, that is 1000x4, to use as an input for a function. I need to add a new column that contains a certain string. So how can I make a new column where all the values are 'TEST'?
Upvotes: 0
Views: 1493
Reputation: 125864
Since it's a little unclear what you want, here are some options:
To make a 1000-by-4 matrix where each row is 'TEST'
, you can use the function REPMAT:
M = repmat('TEST',1000,1);
To add 'TEST'
to the end of each row of a 1000-by-4 matrix of characters, you can use the function STRCAT:
M = repmat('a',1000,4); %# Sample matrix filled with 'a'
M = strcat(M,'TEST'); %# Append 'TEST' to each row of M
If your 1000-by-4 matrix is a numeric array instead of an array of characters, you will have to use cell arrays to combine the different types of data. Here's one way you can do this:
M = rand(1000,4); %# A matrix of random numeric values
M = num2cell(M,2); %# Put each row of M in a cell, making
%# a 1000-by-1 cell array
M(:,2) = {'TEST'}; %# Add a second column to the cell array,
%# where each cell contains 'TEST'
Upvotes: 3
Reputation: 34601
If this is an existing matrix M
of cell strings,
M(:,end+1) = {'TEST'};
Upvotes: 0
Reputation: 1331
A matrix cannot contain a string (like 'TEST'). You need to use a cell array
Upvotes: 0