How to access the n-th caller workspace?

조회 수: 69 (최근 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 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