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 commentaire

abdelkader omr
abdelkader omr le 12 Août 2024
Déplacé(e) : Walter Roberson le 12 Août 2024
I saved figure as follow,but I can't find it in my workspace

Connectez-vous pour commenter.

 Réponse acceptée

Ryan Johnson
Ryan Johnson le 7 Jan 2014

2 votes

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 commentaires

Elvin
Elvin le 7 Jan 2014
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
Ryan Johnson
Ryan Johnson le 7 Jan 2014
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)
Ryan Johnson
Ryan Johnson le 7 Jan 2014
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.
Patrik Ek
Patrik Ek le 7 Jan 2014
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).
Elvin
Elvin le 7 Jan 2014
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 :)
To Minh Tan Le
To Minh Tan Le le 20 Jan 2017
Thanks Ryan.

Connectez-vous pour commenter.

Plus de réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 7 Jan 2014

0 votes

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

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by