Reputation: 55
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:
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?
Upvotes: 3
Views: 368
Reputation: 18177
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.
Upvotes: 3