How to access the n-th caller workspace?

63 ビュー (過去 30 日間)
Jacorem
Jacorem 2024 年 10 月 22 日
コメント済み: Jacorem 2024 年 11 月 4 日 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

採用された回答

埃博拉酱
埃博拉酱 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 Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by