help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Unable to get function changes outside the function.


From: James Sherman Jr.
Subject: Re: Unable to get function changes outside the function.
Date: Tue, 15 Sep 2015 07:03:07 -0400

On Tue, Sep 15, 2015 at 2:38 AM, Manpreet Dhiman <address@hidden> wrote:
> On Tue, Sep 15, 2015 at 2:09 AM, siko1056 [via Octave]
> <address@hidden> wrote:
>> Hello,
>>
>> Did you put the command
>>
>> global Stiffness_matrix;
>>
>> in every m-file you are using the variable 'Stiffness_matrix'?
>
> Sir, I want to use value of  "Stiffness_matrix" at somewhere in same file.
> For example:
> When I used  " matrixTeX(Stiffness_matrix,'%10.4e','r') " for creating
> latex form.
> it does not show Stiffness_matrix value.
>
>
>
> --
> Manpreet Dhiman.
> Gmail: address@hidden
> Blog: https://manpreet9112.wordpress.com
>
>
>
>
> --
> View this message in context: 
> http://octave.1599824.n4.nabble.com/Unable-to-get-function-changes-outside-the-function-tp4672565p4672576.html
> Sent from the Octave - General mailing list archive at Nabble.com.
>
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://lists.gnu.org/mailman/listinfo/help-octave

Hi Manpreet,

I think you seem to be confusing the difference between a function
file and a script file.  While there may be a way to do it another
way, the conventional way to define a function is to give it its own
separate file (this is at least how I learned it since its how MATLAB
does it, but octave may have improved upon this "feature").  So, say I
wanted a function called foo.  I would need to put it inside its own
file, called foo.m:
-------
function y = foo(x)
y = exp(x)-x.^2;
endfunction
-------
Now if I wanted to call foo to use it inside a script, I'd do
something like this, inside a file called bar.m:
--------
x = 2;
y = foo(x);
disp(sprintf('x = %f, y = %f', x, y));
--------

Now one way that it might work in one file ( I can't test it now),  is
at the end of your file:
------
disp(Stiffness_matrix);  //not work, display zeros.

stif(Stiffness_storey); //calling
------
Is that you try displaying it before you actually call your function.
This will definitely not work, what it would need to do is call the
function first, then you could display the values:
------
stiff_matrix = stif(Stiffness_storey); //calling

disp(stiff_matrix);  //not work, display zeros.
------

This might work without having to use global variables.  It'll
definitely work if you save your stif function in a different file
(call it stif.m) and then do the following in another file:
------
Number_of_storeys=4;
stiff_matrix = stif(Number_of_storeys);
disp(stiff_matrix);
------

Hope this helps.

James Sherman



reply via email to

[Prev in Thread] Current Thread [Next in Thread]