From: Jean-Philip Dumont on
Hi,

I'm trying to create a loop that will generate ma variable names automatically.

Here's my code : (Titres means Titles)

Titres2 = num2cell(zeros(1+p*size(A3,2),3));
Titres2{1,1} = 'const';
Titres2{1,2} = 'const';
Titres2{1,3} = 'const';

Titres = Data.textdata(7,2:4);

for i=1:size(A3,2)
for j=1:size(Titres,2)
Titres2(1+((i+j)-1)+((2*i)-2),:) = {Titres{i}, '_t-', j};
end
end

Basically, "i" goes from 1 to 3 and "j" goes from 1 to 3 and "Titres" is a cell of this form : {'P' 'D' 'E'};

The only output I was able to get is [P, _t-, 1] in three different cells. In the end I have a matrix 10x3. I would like to create a character form that will create "P_t-1" in only one cell (10x1) like if you type : X = ['P', '_t-', '1']

My only problem is my "j" in the loop. Because it is a number and considered an index, I cannot have the character format I would like to have.

Thank you very much for your help!

Jean-Philip
From: us on
"Jean-Philip Dumont" <jean-philip.dumont(a)hec.ca> wrote in message <hv09bi$h4q$1(a)fred.mathworks.com>...
> Hi,
>
> I'm trying to create a loop that will generate ma variable names automatically.
>
> Here's my code : (Titres means Titles)
>
> Titres2 = num2cell(zeros(1+p*size(A3,2),3));
> Titres2{1,1} = 'const';
> Titres2{1,2} = 'const';
> Titres2{1,3} = 'const';
>
> Titres = Data.textdata(7,2:4);
>
> for i=1:size(A3,2)
> for j=1:size(Titres,2)
> Titres2(1+((i+j)-1)+((2*i)-2),:) = {Titres{i}, '_t-', j};
> end
> end
>
> Basically, "i" goes from 1 to 3 and "j" goes from 1 to 3 and "Titres" is a cell of this form : {'P' 'D' 'E'};
>
> The only output I was able to get is [P, _t-, 1] in three different cells. In the end I have a matrix 10x3. I would like to create a character form that will create "P_t-1" in only one cell (10x1) like if you type : X = ['P', '_t-', '1']
>
> My only problem is my "j" in the loop. Because it is a number and considered an index, I cannot have the character format I would like to have.
>
> Thank you very much for your help!
>
> Jean-Philip

show the output of your TITRES...

us
From: Walter Roberson on
Jean-Philip Dumont wrote:


> Titres2(1+((i+j)-1)+((2*i)-2),:) = {Titres{i}, '_t-', j};

Titres2(1+((i+j)-1)+((2*i)-2),:) = {[Titres{i} '_t-' sprintf('%d',j)]};
From: Jean-Philip Dumont on
Walter Roberson <roberson(a)hushmail.com> wrote in message <f2OQn.37431$rU6.15222(a)newsfe10.iad>...
> Jean-Philip Dumont wrote:
>
>
> > Titres2(1+((i+j)-1)+((2*i)-2),:) = {Titres{i}, '_t-', j};
>
> Titres2(1+((i+j)-1)+((2*i)-2),:) = {[Titres{i} '_t-' sprintf('%d',j)]};


Wow! Thank you so much it works perfectly!
From: dpb on
Jean-Philip Dumont wrote:
> Walter Roberson <roberson(a)hushmail.com> wrote in message
> <f2OQn.37431$rU6.15222(a)newsfe10.iad>...
>> Jean-Philip Dumont wrote:
>>
>>
>> > Titres2(1+((i+j)-1)+((2*i)-2),:) = {Titres{i}, '_t-', j};
>>
>> Titres2(1+((i+j)-1)+((2*i)-2),:) = {[Titres{i} '_t-' sprintf('%d',j)]};
>
>
> Wow! Thank you so much it works perfectly!


How about

Titres2(3*i+j-2,:) = {[Titres{i} '_t-' sprintf('%d',j)]};

??

:)

--