Date: Feb 20, 2013 2:42 PM
Author: Curious
Subject: Re: variable number of inputs of a function

"vijay " <vijaybrcm@rediffmail.com> wrote in message <kg362o$6eh$1@newscl01ah.mathworks.com>...
> function []=mainprog()
> clc
> len=5
>
> for i=1:len
> eval(['scalar', num2str(i), '= randi(10,1,3)']);
> end
> rqarray=[10,20,30,40,50]
> weighted=weightedimage(scalar,rqarray)
>
>
> function weighted=weightedimage(scalar,rqarray);
> for i=1:len
> w=rqarray(1,i)
> J=w*[scalar]
> allzero=zeros(1,3)
> wfinal=allzero+w
> Jfinal=allzero+J
> end
> weighted=Jfinal./wfinal
>
>
>
>
> In this program in the function weightedimage, how we should write input named as scalar. if i write it as scalar it gives error.


What error message does it give you?

I don't have MATLAB installed on this computer, but in general, I would avoid using scalar as a variable. It may be a keyword reserved by MATLAB. Try changing scalar to something like Scalar, scAlar, or something else and see if that works. Also, you should avoid using eval in the above code. Use cell arrays instead. Simply:

scalar1{i} = randi(10,1,3);

should work.

See the MATLAB FAQ at:

<<http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F>>

for more info.