MATLAB: How to save data from a loop to a changing row and column

counter looperrorloop

i am trying to save 10 patient data by using a counter loop which change row and column location each loop to save in matlab them sent to excel but i keep getting ' Subscripted assignment dimension mismatch' , any ideas this is my code :
count=1;
a=[];
for pat=1:10
prompt='input age';
age=input(prompt)
prompt ='input weight';
weight=input(prompt )
prompt='input hight';
hight=input(prompt)
prompt='input gender F/M';
sex=input(prompt)
b=[age,weight,hight,sex];
a(pat,count)=b;
count=count+1;
end

Best Answer

Well, there are two possible options for you in this case:
If you want to save b vector as a unit, then you need to use cell array as follows:
count=1;
for pat=1:10
prompt='input age';
age=input(prompt)
prompt ='input weight';
weight=input(prompt )
prompt='input hight';
hight=input(prompt)
prompt='input gender F/M';
sex=input(prompt)
b=[age,weight,hight,sex];
a{pat,count}=b;
count=count+1;
end
but in this case, you will only use diagonal elements of the array, which does not make sense but you can change it if you want.
Secondly, you can use double array but then you need to get rid of count variable since b vector will be written to the row of the a matrix, as follows:
for pat=1:10
prompt='input age';
age=input(prompt)
prompt ='input weight';
weight=input(prompt )
prompt='input hight';
hight=input(prompt)
prompt='input gender F/M';
sex=input(prompt)
b=[age,weight,hight,sex];
a(pat,:)=b;
count=count+1;
end