How to make available initialization variables in workspace

Hello,
when a script, after declaring variables, I create a loop (eg. while) they are available in the workspace at the end of the loop
Is there a way to make them available since the execution of the script?

2 Commenti

I think I understand your question. You're wondering why the workspace variables are not displayed in the Workspace during the loop execution. The reason is that the Workspace is not refreshed until the script is finished completion. Refreshing the Workspace during script/function execution is a feature that I've also wished for!
I know that if in a while loop I need to return a value I can use "return" command...maybe I have to create/initialize variable before run (???)

Accedi per commentare.

Risposte (2)

You probably want to set a debug point somewhere in the while loop - either use the debug command or click the tick mark next to the line number. The program will pause at that point and give access to all variables in the current workspace. Here's a video to get you started.

1 Commento

I guess a breakpoint may be my solution, but... is there a way to automate the Continue (F5) button inserting it in the code with the condition?

Accedi per commentare.

If you have a script, the variables will remain in the base workspace:
File test.m:
a = 42;
b = 'These will hang around';
If you have a function, then will vanish when your m-file finishes:
File test.m:
function test()
a=42;
b = 'These will NOT hang around';
Note that the "function" line in the second file turns it from a script into a function, and that means that the variables are local to that function and will vanish when the function exits. If it's a script, like in the first case, the variables will still be there for you to inspect in the base workspace.
Understand?

11 Commenti

So, if I have a script with:
a=1;
b=5;
while -condition-
.....etc....
end
though I don't see variables in the workspace tab ('cause I would wait loop exit), they are in there, is it?
If you have that script, and there is no "function" line in there, and you don't have any call to "clear" and you don't call clear at the command line, then those variables get created when they first are used, and they will remain in the base workspace when your script finishes.
and is there not a way to display them in the base workspace since they get created during script execution (like a base refresh) ?
How can I know they real time value?
What does "real time" mean to you? At any time you want (after your script has run), you can type the variable name onto the command line and it will report it's value to the command window. Or you can use the variable editor (double click variable name in the workspace panel) to examine, and change if desired, the variable.
Once your script exits, there will be no more updating of the variable so it doesn't really make sense to repeatedly print it out somehow or somehow otherwise inspect it or continuously report its value in real time.
Marco
Marco il 28 Set 2012
Modificato: Marco il 28 Set 2012
I mean that during a while loop execution variables are not visible in the workspace and so I can not check them values (instantaneous value) in the workspace window.
They are visible only at the end of the while loop and so at the end of the execution. I need their values during "Busy" time.
Do you understand now?
To do that you either display them to the command window inside your loop (by either just putting the name on its own line of code or by using disp(), fprintf(), or celldisp() functions), OR you can have a gui and use sprintf(), set() and drawnow to update some label on your gui (again, update the label inside the loop)
Marco
Marco il 29 Set 2012
Modificato: Marco il 29 Set 2012
Ok, and this for display them in the command windows...and if would I have them in the workspace tab?
Type "workspace" on the command line, or as a line of code in your program and the workspace panel will become visible and you'll be able to see all the variables in the current workspace. You do know that there are different workspaces don't you? The "base" workspace, plus each function has its own local workspace.
Where can I find this in the help?
I wrote "workspace" in the script as, for example:
A=2;
B=3;
M=magic( abs(int32(random('Normal',0,1)))+1);
workspace
while -loop condition-
if M(1,1)>2
A=1;
workspace
else A=20;
workspace
end
pause(1)
end
In other words, I wrote "workspace" every time I want update the workspace but it doesn't update base workspace for changes done in the loop
The workspace should get updated every iteration. workspace simply shows the panel - it doesn't force the panel to update. You definitely don't need it inside your loop! If you're in a really intensive tight loop and you find that the workspace panel does not immediately show the changing values, you can try to put a "drawnow" in your loop to try to force the user interface to update, or else display the values in the command window or a GUI like I said earlier. If you use a custom GUI, and it's in a tight loop, then you're going to have to use "drawnow" in the loop to get the GUI to update on every iteration. Otherwise, message are just getting sent there but they're just queued up and don't get acted upon until Windows gets around to it (that's how multitasking operating systems work).
Ok, I understand, thanks!

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Prodotti

Richiesto:

il 27 Set 2012

Community Treasure Hunt

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

Start Hunting!

Translated by