How to access the n-th caller workspace?

62 views (last 30 days)
Jacorem
Jacorem on 22 Oct 2024
Commented: Jacorem on 4 Nov 2024 at 10:41
I have a question concerning the problem represented by this diagram (black arrow represent function calls):
Where utilityClass.getOrigClasspath() should get access to the workspace of "mainClass.doStuffTroughtUtilityClass()" so to access the "obj" variable.
The code for "mainClass" is more or less:
classdef mainClass < handle
methods
function doStuffTroughtUtilityClass(obj)
utilityClass.doStuff1();
% and/or
utilityClass.doStuff2();
end
end
methods (Static)
function filePath=getObjPath()
mFullFile=mfilename("fullpath");
[filePath,~,~]=fileparts(mFullFile);
% + all the code to handle the different cases: deployed
% application, different OS, ...
end
end
end
and the one for the utilityClass:
classdef utilityClass
methods (Static)
function doStuff1()
origClassPath=utilityClass.getOrigClassPath();
% do some stuff
end
function doStuff2()
origClassPath=utilityClass.getOrigClassPath();
% do some other stuff
end
function pathOutMod=getOrigClassPath()
origClassPath=evalin("2-nd parent","obj.getObjPath()"); % this is the command I'm looking for
% and then I modify somehow the output
pathOutMod=sprintf("%s modified",origClassPath);
end
end
end
I would like to have the methods "doStuff1" and "doStuff2" in "utilityClass" because:
  1. I have several projects (that do different operations), each with the "getObjPath()" method (I like to use the same function name in case different functions of different classes should give similar results), and I would like to give all of them new functionalities (doStuff1 and doStuff2)
  2. it make sense that these new functionalities are managed by "utilityClass" (since they are related to such class)
So I would like to keep this structure.
The problem now is what I stated above: how to access the "mainClass.doStuffTroughtUtilityClass()" workspace from "utilityClass.getOrigClasspath()".
My idea was to use "evalin", but it is limited to the direct "parent" workspace (and the "base" one, but it does not help) and recursively calling it is discouraged (see evalin documentation) and it does not work in any case (I tried).
Do you have any idea how to dela with this problem?
The workaround I tought about is to pass "obj" at each function call, but I would like to avoid it for several reasons.
Thanks in advance,
Jacopo

Accepted Answer

埃博拉酱
埃博拉酱 on 28 Oct 2024 at 11:47
  2 Comments
Jacorem
Jacorem on 28 Oct 2024 at 12:54
Deart 埃博拉酱, actually dbstack may more or less do the trick. I will need some more time to testing it, especially in a deployed environment, but once done I will come back and, in case, accept the answer
Jacorem
Jacorem on 4 Nov 2024 at 10:41
Dear 埃博拉酱, I finally had time to test your solution and it seems to work. Thanks again for the suggestion!

Sign in to comment.

More Answers (1)

Matt J
Matt J on 27 Oct 2024 at 1:33
Since getObjPath() is a Static method, why not just do,
function pathOutMod=getOrigClassPath()
origClassPath=mainClass.getObjPath();
pathOutMod=sprintf("%s modified",origClassPath);
end
  1 Comment
Jacorem
Jacorem on 28 Oct 2024 at 8:04
Dear Matt, the fact is that once I want to get the path of "mainClass", sometimes "mainClass2", other "mainClass3",... (all that has the same "getObjPath()" static method) so hard-coding it does not solve my problem

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!