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 件のコメント

abdelkader omr
abdelkader omr 2024 年 8 月 12 日
移動済み: Walter Roberson 2024 年 8 月 12 日
I saved figure as follow,but I can't find it in my workspace

サインインしてコメントする。

 採用された回答

Ryan Johnson
Ryan Johnson 2014 年 1 月 7 日

2 投票

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 件のコメント

Elvin
Elvin 2014 年 1 月 7 日
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 2014 年 1 月 7 日
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?
Elvin
Elvin 2014 年 1 月 7 日
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 2014 年 1 月 7 日
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 2014 年 1 月 7 日
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 2014 年 1 月 7 日
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 2017 年 1 月 20 日
Thanks Ryan.

サインインしてコメントする。

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 7 日

0 投票

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

カテゴリ

ヘルプ センター および File ExchangeWorkspace Variables and MAT Files についてさらに検索

質問済み:

2014 年 1 月 7 日

移動済み:

2024 年 8 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by