Generating multiple data and putting them in a cell array

2 views (last 30 days)
I tried using a code to generate 500 sets of data i.e.S1, S2,S3,...,S500 which can then be put in a cell array to be used for a calculation. It worked up to the 3rd data and gave me an error message: "Subscripted assignment dimension mismatch". The code is given below. The function 'hss' used in the code is attached.
N =500; k = 1;
Xk = hss(N,k);
r = Xk;
x_hss=r;
X=x_hss; %500 data points to be used in 3rd columns
FR=[0.01;0.11;0.19;0.34];
XD=[0;1;2;3;4;5;7];
for i=1:500,
eval(sprintf('XD%d=repmat(XD,[1 1])', i)); %generates 1st column
eval(sprintf('FR%d=repmat(FR(i,:),[7 1]),',i)); %generates 2nd column
eval(sprintf('Cu%d = repmat(X(i,:),[7 1])', i)); %generates 3rd column
XD(1:7,i)=eval(sprintf('XD%d',i));
FR(1:7,i)=eval(sprintf('FR%d',i));
Cu(1:7,i)=eval(sprintf('Cu%d',i));
S(1:7,i)=eval(sprintf('XD(1:7,i)','FR(1:7,i)','Cu(1:7,i)',i)); %tries to combine 1st,2nd&3rd %column
end
After generating the sets of data i.e. S1,S2,S3,S4,...,S500, these will be put in a cell array. Please how do I create a loop to generate this for me rather than typing them out which will be cubersome? Thanks.*

Accepted Answer

Stephen23
Stephen23 on 1 Dec 2014

More Answers (1)

Guillaume
Guillaume on 27 Nov 2014
Don't, don't, don't! Don't use eval to generate variable names.
As you've discovered, there's no way you can type manually 500 x 3 different variables names. So what's the point of having so many variables. Use the proper way and stuff all these in a cell array to start with.
Moreover, with eval, you lose the editor syntax checking, the ability to set breakpoint on an expression and make the expression harder to read (try putting a transpose in there!). Lose the evals:
cXD{i} = repmat(XD, [1 1]); %that repmat doesn't do anything
cFR{i} = repmat(FR(i, :), [7 1]);
Cu({i} = repmat(X(i, :), [7 1]);
XD(1:7, 2) = cXD{i}; %??? Your original eval wasn't going to work with i>=1, neither is this
%... ??? None of the lines following make any sense.

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!