From: Dave400 on
So, I have a problem that i hope a regular user can help me with.

I have 1400 variables in my workspace named ‘a_1, a_2, a_3’ and so on.

I would like to be able to group these into a single variable named &#8216;a_total&#8217; which includes the 1400 single values, i.e a_total = <1x1400 double>.

I have tried using a for loop but pretty sure I am missing something as I get an error message

??? Undefined function or method 'a_' for input arguments of type 'double'.

j=1;
for j = 1:1400
a_total = a_(j);
end

any help resolving this issue would be very much appreciated as a manual process would take ages.

Regards,

DR
From: Andy on
"Dave400 " <dave.rogers(a)hotmail.co.uk> wrote in message <i440bh$j3q$1(a)fred.mathworks.com>...
> So, I have a problem that i hope a regular user can help me with.
>
> I have 1400 variables in my workspace named &#8216;a_1, a_2, a_3&#8217; and so on.
>
> I would like to be able to group these into a single variable named &#8216;a_total&#8217; which includes the 1400 single values, i.e a_total = <1x1400 double>.
>
> I have tried using a for loop but pretty sure I am missing something as I get an error message
>
> ??? Undefined function or method 'a_' for input arguments of type 'double'.
>
> j=1;
> for j = 1:1400
> a_total = a_(j);
> end
>
> any help resolving this issue would be very much appreciated as a manual process would take ages.
>
> Regards,
>
> DR

A few questions:

1) Are the a_n the only variables in the workspace?
2) Why did you create them all in the first place? Why not start with a_total?
3) Have you read the FAQ, specifically question 4.6: http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
From: Matt J on
"Dave400 " <dave.rogers(a)hotmail.co.uk> wrote in message <i440bh$j3q$1(a)fred.mathworks.com>...
> So, I have a problem that i hope a regular user can help me with.
>
> I have 1400 variables in my workspace named &#8216;a_1, a_2, a_3&#8217; and so on.

As Andy said, the fact that you are in this situation is suspicious. However, this should do the concatenation for you

a_total = [arrayfun(@(x)evalin('caller', ['a_' num2str(x)]),1:1400)];
From: Dave400 on
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <i441m4$f73$1(a)fred.mathworks.com>...
> "Dave400 " <dave.rogers(a)hotmail.co.uk> wrote in message <i440bh$j3q$1(a)fred.mathworks.com>...
> > So, I have a problem that i hope a regular user can help me with.
> >
> > I have 1400 variables in my workspace named &#8216;a_1, a_2, a_3&#8217; and so on.
>
> As Andy said, the fact that you are in this situation is suspicious. However, this should do the concatenation for you
>
> a_total = [arrayfun(@(x)evalin('caller', ['a_' num2str(x)]),1:1400)];

thank you both.

Matt, that works great

out of interest i have attached my message before you sen yours, so any tips on this would be good also

I am generating 7000 values and want to conduct a convergence study on the impact of the number of samples used.

So I wanted to grab the first 5 then the next and so on until I had 1400 variables each with 5 values present. I didn&#8217;t know how to group them in the loop so though it was best to do a separate loop on the variables to avoid confusion.

My original code is below


M = <1x7000 double>

b = 5;
c = 1;
a = zeros(1400,5);
for k = 1:1400
a(k,:) = M(idx(1,c:b));
b = b+5;
c = c+5;
d(k,:) = mean(a(k,:));
name='a_';
vn=sprintf('%s%1.1d',name,k);
eval([vn '(:,:) = [d(k,:)]; '])
end

this generated variables a_1, a_2 a_3 and so on. When I tried adding the extra stage in the final value was the only one that remained instead of incrementing each on in a single variable name.
From: Matt J on
"Dave400 " <dave.rogers(a)hotmail.co.uk> wrote in message <i442d5$114$1(a)fred.mathworks.com>...

> b = 5;
> c = 1;
> a = zeros(1400,5);
> for k = 1:1400
> a(k,:) = M(idx(1,c:b));
> b = b+5;
> c = c+5;
> d(k,:) = mean(a(k,:));
> name='a_';
> vn=sprintf('%s%1.1d',name,k);
> eval([vn '(:,:) = [d(k,:)]; '])
> end
========

You could have just done

a= reshape( M(idx(1,:)), 5,[]) ).';
d=mean(a,2);
a_total=d;