Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Octave: How do I examine variables created in a seperate .m file?

I have 2 .m files, for an example say "project_main.m" and "my_function.m"

When I run the program in Octave it works fine. However the only variables I can see in the workspace area are those created in project_main.m, I would like to examine variables created in my_function.m from the workspace area.

Example:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

project_main.m:

close all;
clear all;
clc;

X = 1;
Y = 2;

%---call function in external .m file---
my_function(X, Y);

my_function.m:

function functionResults = my_function(iX, iY)

fprintf("X = %d, Y = %d\n", iX, iY);
Z = iX*iY;
fprintf("Z = %d\n", Z);

end

This will give me on the command window:

X = 1, Y = 2
Z = 2
>>

As expected. However in the workspace area I only see X and Y, not Z:
I know I can see Z on the command window, but how do I examine variables created in separate .m files from the workspace area?

>Solution :

You’ll need a debugger. This documentation page seems like a good start. Either run your function line-by-line using the command window, or set breakpoints to return the current function workspace to the global one.

Basically: variables within functions are private to that function and only the ones you output are copied into your workspace. In your case that is nothing, since fprintf doesn’t return the output, it merely prints to the command window.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading