Question

I have a string (eg. 'STA') and I want to make a cell array that will be a concatenation of my sting with a numbers from 1 to X.

I want the code to do something like the fore loop here below:

for i = 1:Num
    a = [{a}  {strcat('STA',num2str(i))}]
end

I want the end results to be in the form of {<1xNum cell>}

a = 'STA1' 'STA2' 'STA3' ...

(I want to set this to a uitable in the ColumnFormat array)

ColumnFormat = {{a},...                 % 1
             'numeric',...              % 2
             'numeric'};                % 3
Était-ce utile?

La solution 2

You can do it with combination of NUM2STR (converts numbers to strings), CELLSTR (converts strings to cell array), STRTRIM (removes extra spaces)and STRCAT (combines with another string) functions.

You need (:) to make sure the numeric vector is column.

x = 1:Num;
a = strcat( 'STA', strtrim( cellstr( num2str(x(:)) ) ) );

As an alternative for matrix with more dimensions I have this helper function:

function c = num2cellstr(xx, varargin)
%Converts matrix of numeric data to cell array of strings

c = cellfun(@(x) num2str(x,varargin{:}), num2cell(xx), 'UniformOutput', false);

Autres conseils

I'm not sure about starting with STA1, but this should get you a list that starts with STA (from which I guess you could remove the first entry).

N = 5;
[X{1:N+1}] = deal('STA');
a = genvarname(X);
a = a(2:end);

Try this:

N = 10;
a = cell(1,N);
for i = 1:N
    a(i) = {['STA',num2str(i)]};
end
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top