How to access the n-th caller workspace?

63 次查看(过去 30 天)
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

采纳的回答

埃博拉酱
埃博拉酱 2024-10-28,11:47
  2 个评论
Jacorem
Jacorem 2024-10-28,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 2024-11-4,10:41
Dear 埃博拉酱, I finally had time to test your solution and it seems to work. Thanks again for the suggestion!

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2024-10-27,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 个评论
Jacorem
Jacorem 2024-10-28,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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by