declare global as matrix...problem to use it

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

declare global as matrix...problem to use it

turbofib
hi,
i declare a global in the main program(it is a matrix )

global a;

a=[1 12 30;3 5 50;6 3 2]


i use it in a function but i get the following error

error: 'a' undefined near line 3 column 8
error: called from
    mainGlobal at line 3 column 7











--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: declare global as matrix...problem to use it

Doug Stewart-4


On Tue, Jul 3, 2018, 9:38 AM turbofib, <[hidden email]> wrote:
hi,
i declare a global in the main program(it is a matrix )

global a;

a=[1 12 30;3 5 50;6 3 2]


i use it in a function but i get the following error

error: 'a' undefined near line 3 column 8
error: called from
    mainGlobal at line 3 column 7








You must declare it global in both places.



Reply | Threaded
Open this post in threaded view
|

Re: declare global as matrix...problem to use it

turbofib
hi doug,

i try it :


Main.m:

global a;

a=[1 12 30;3 5 50;6 3 2]


*******************
function ret = eserciziGlobal_Funzioni(x)

 global a;

   ret=a;

endfunction

*****************
Here..i call function:

>> eserciziGlobal_Funzioni(3)

i receive the error:

error: 'a' undefined near line 6 column 8
error: called from
    eserciziGlobal_Funzioni at line 6 column 7



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: declare global as matrix...problem to use it

Doug Stewart-4


On Tue, Jul 3, 2018 at 10:44 AM, turbofib <[hidden email]> wrote:
hi doug,

i try it :


Main.m:

global a;

a=[1 12 30;3 5 50;6 3 2]


*******************
function ret = eserciziGlobal_Funzioni(x)

 global a;

   ret=a;

endfunction

*****************
Here..i call function:

>> eserciziGlobal_Funzioni(3)

i receive the error:

error: 'a' undefined near line 6 column 8
error: called from
    eserciziGlobal_Funzioni at line 6 column 7


if you make one file with this in it:

function ret = eserciziGlobal_Funzioni(x)
 global a; 
   ret=a;
endfunction

and name it  eserciziGlobal_Funzioni.m


and a second file with this in it


global a;
a=[1 12 30;3 5 50;6 3 2]
eserciziGlobal_Funzioni(3)

and name it testglobal.m

and then at the command prompt type

testglobal

It works for me.



--
DAS



Reply | Threaded
Open this post in threaded view
|

Re: declare global as matrix...problem to use it

LucaLuca
hi,
i try to use global with array string but it give me error:

global string_dat_rp

string_dat_rp(1,:)={"xiao"};

error: operator =: no conversion for assignment of 'cell' to indexed
'matrix'





--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: declare global as matrix...problem to use it

Mike Miller-4
On Fri, Jul 06, 2018 at 15:24:08 -0700, LucaLuca wrote:
> i try to use global with array string but it give me error:
>
> global string_dat_rp
>
> string_dat_rp(1,:)={"xiao"};
>
> error: operator =: no conversion for assignment of 'cell' to indexed
> 'matrix'

I think you want this instead

    global string_dat_rp
    string_dat_rp = {"xiao"};

--
mike



signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: declare global as matrix...problem to use it

LucaLuca
hi mike…
i want an array of string..not a single string




--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: declare global as matrix...problem to use it

Mike Miller-4
On Sat, Jul 07, 2018 at 00:53:22 -0700, LucaLuca wrote:
> i want an array of string..not a single string

You showed that you wanted an array with a single string, so it's hard
to infer from that what you actually want.

If you want an array of strings, then maybe this will work

    global x
    x = {"one", "two", "three"};

Or maybe you need to add them one at a time, like this

    global x
    x = {"one"};
    x(2) = "two";
    x(3) = "three";

https://octave.org/doc/interpreter/Cell-Arrays-of-Strings.html

--
mike



signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: declare global as matrix...problem to use it

turbofib
hi mike…

thank you for answer

if i code :

string_dat_rp(1)=" example ";

i receive this error:

 error: TitanFiltri2: A(I) = X: X must have the same size as I
error: called from



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: declare global as matrix...problem to use it

Doug Stewart-4


On Sun, Jul 8, 2018, 4:44 AM turbofib, <[hidden email]> wrote:
hi mike…

thank you for answer

if i code :

string_dat_rp(1)=" example ";

i receive this error:

 error: TitanFiltri2: A(I) = X: X must have the same size as I
error: called from




What is TitanFiltri2  ?





Reply | Threaded
Open this post in threaded view
|

Re: declare global as matrix...problem to use it

turbofib
HI,

in this function i code it:  string_dat_rp(1)=" example ";




--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: declare global as matrix...problem to use it

Doug Stewart-4


On Sun, Jul 8, 2018 at 8:37 AM, turbofib <[hidden email]> wrote:
HI,

in this function i code it:  string_dat_rp(1)=" example ";




try
string_dat_rp(1,:)=" example"
--
DAS



Reply | Threaded
Open this post in threaded view
|

Re: declare global as matrix...problem to use it

Mike Miller-4
In reply to this post by turbofib
On Sun, Jul 08, 2018 at 01:43:50 -0700, turbofib wrote:
> if i code :
>
> string_dat_rp(1)=" example ";
>
> i receive this error:
>
> error: TitanFiltri2: A(I) = X: X must have the same size as I
> error: called from

I know, that's why you need to create string_dat_rp as a cell array.

It should be clear that this error is because string_dat_rp is a matrix,
it needs to be a cell array. I already gave you a couple of different
ways to do that.

--
mike



signature.asc (849 bytes) Download Attachment