Info

This question is closed. Reopen it to edit or answer.

Can you clarify why the FOR END returns original values just at the end ?

1 view (last 30 days)
When debugging my program, Matlab is is doing OK. The following variables are created in the workspace:
p=full(porosity); kequiz=full(kx); kye=full(ky);
But just in the last step of the debugging, just to finish the last END (see where the @ is below), my program is disappearing the variables p,kequiz and kye. Not only that, the arrays
porosity(counter) = 0;
kx(counter) = 0;
ky(counter) = 0;
are returned to its original values without the modifications done and reviewed thought out the debugging process.
Please any idea. Thanks in advance.
----------------------------------------------------
function [i,j,s,indices,porosity,kx,ky,counter,xs,ys,p,kequiz,kye]= processactinactive(Loadedcells,porosity,kx,ky,nx,ny)
% This sections identifies the active cells and modifies Kx, Ky and prosity % values to account for only active cells.
% 1) Creating NEW index list
[i,j,s]=find(Loadedcells); indices=find(Loadedcells);
% 2) Modifying Porosity and permeability
for ys = 1:ny for xs = 1:nx counter = xs+(nx*(ys-1)); if Loadedcells(xs,ys)==0; porosity(counter) = 0; kx(counter) = 0; ky(counter) = 0; end end end
p=full(porosity); kequiz=full(kx); kye=full(ky);
end (@)

Answers (1)

Carsci
Carsci on 19 Feb 2014
Edited: Carsci on 19 Feb 2014
This is how matlab works, variables are scoped inside a function so each function has its own workspace. When you are debugging the function, the workspace window shows the local variables of the currently executing code. At end (@) the workspace window returns to the base matlab workspace and clears the local variables.
To get the result you need there are 2 options:
  1. Assign the values returned by the function to variables in the function call
  2. Use Eval inside the function to write to the base workspace (or parent function's workspace).
I recommend 1. Furthermore from your function it appears there is no need to return the loop counters and some other intermediate values so you should replace
function [i,j,s,indices,porosity,kx,ky,counter,xs,ys,p,kequiz,kye]= ...
with what you actually require after end(@) e.g.
function [p,kequiz,kye]= processactinactive(Loadedcells,porosity,kx,ky,nx,ny)
Then the function call becomes:
[porosity,kequiz,kye]= processactinactive(Loadedcells,porosity,kx,ky,nx,ny)
You will then get values assigned to porosity, kequiz and kye.
If you want to conditionally assign the variables then call the function without return values (as you are doing now) and use Eval. Note that this will make the code more difficult to maintain.

Products

Community Treasure Hunt

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

Start Hunting!