MATLAB not saving variables to workspace

I don't know what's wrong with my MATLAB. Every time I run the dummy.m using the F5 in the editor, all the variables are being displayed in the workspace. But when I run the NitrogenDef.m using again the F5 in the editor, all the variables used in the NitrogenDef.m are not displayed in the workspace. Any help with this? Thanks!

1 Comment

I saved figure as follow,but I can't find it in my workspace

Sign in to comment.

 Accepted Answer

That's because NitrogenDef is a function, so any variables created within it are only scoped for the life of the function (unless made global).
Comment out the first line function NitrogenDef() and it will run as a script.

7 Comments

But I'm running the NitrogenDef before and the variables are being displayed in the workspace. As far as I remember is that there's a popup window after I pressed the F5 before but I didn't remember what the popup is about because I pressed the Enter immediately. Then after that, I can't display my variables in the workspace
Just a guess, but the pop-up may have been saying that NitrogenDef wasn't in your path, and asking if you wanted to add it to your path.
Did you try commenting out the first line?
I tried that but I get an error:
Error: File: NitrogenDef.m Line: 161 Column: 1
Function definitions are not permitted in this context.
Line 161 is also another function. This is the Line 161:
function binaryImage = ExtractNLargestBlobs(binaryImage, numberToExtract)
Ah, right. So your options are to either move that Blobs function out into its own file, or to keep NitrogenDef as a function and add a breakpoint to the end of the function (that way when you run, you'll stop in the function with all of your variables visible).
But really, it seems like based on what you're doing, you want the main NitrogenDef to be a script, not a function anyway. It takes in no inputs and returns no results. So I'd recommend the first option. There are other options too, but keep it simple.
Functions are not allowed in scripts. You can call a function from a script, but then the function needs to be in another file with the same name as the function. This should also be the recommended procedure. Comment the function line at the top and then move all the functions to separate files (unless a function is only called by another function, a so called subfunction).
It's now okay. I just cut the Blobs function and paste it to a new editor and save it sa Blobs.m. And on the NitrogenDef, I already commented the first line.
Thank you very much for the help :)

Sign in to comment.

More Answers (1)

They are different type of file : script file and function file

Asked:

on 7 Jan 2014

Moved:

on 12 Aug 2024

Community Treasure Hunt

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

Start Hunting!