How can I get the path of the file being loaded when implementing the loadobj method

11 views (last 30 days)
When implementing a load object method with:
a = loadobj(b)
I would like to be able to get the path of the orinial file which caused this object to load.
Example code:
classdef MockObj
methods (Static)
function obj = loadobj(s)
FilePath = SolutionCode();
disp("The obj was loaded from " + FilePath );
obj = s;
end
end % methods
end % classdef
When loading a saved file containing an instance of MockObj, I would like to print the location of this file, from within the method.
I'm missing the
SolutionCode()
which will give me the fullpath of the file which caused MockObj to load.
Edited (2020-08-26):
For clarifications:
The file is the .mat file which MATLAB creates when you save your workspace.
Once it is saved, you can move your file to whatever folder.
When you load a file that contains an instance of MockObj class, the loadobj(s) method is being called.
Unfortunately, once you’re inside the method, it seems as if you’ve lost any information about the .mat file. You can only access the variable s, which is the object itself.
That’s what I’m looking for.. A way to figure out something about the .mat file which caused the method loadobj(s) of my MockObj class.
Any other solution is also blessed.
Thanks in advance,
Nir G.

Answers (2)

per isakson
per isakson on 19 Aug 2020
Edited: per isakson on 19 Aug 2020
A little demo
>> clearvars
>> create_and_save_mock_obj
>> load_mock_obj
The obj was loaded from D:\m\cssm\load_mock_obj.m
so far so good
>> load mock_obj.mat
Index exceeds array bounds.
Error in SolutionCode (line 3)
ffs = which(stk(1).file);
Error in MockClass.loadobj (line 4)
FilePath = SolutionCode();
a test is needed whether the variable stk is empty; loading from the base workspace.
Code
function ffs = SolutionCode()
stk = dbstack(2);
ffs = which(stk(1).file);
end
function create_and_save_mock_obj
mock_obj = MockClass();
save('mock_obj.mat', 'mock_obj' );
end
function load_mock_obj
S = load('mock_obj.mat'); %#ok<*NASGU>
end
and MockClass is identical to you MockObj
  6 Comments
Nir Gutman
Nir Gutman on 24 Aug 2020
I'm sorry that it was confusing, and I hope the following explanation might clear thing up:
As You’ve guessed, the file is the .mat file which MATLAB creates when you save your workspace.
Once it is saved, you can move your file to whatever folder.
When you load a file that contains an instance of MockObj class, the loadobj(s) method is being called.
Unfortunately, once you’re inside the method, it seems as if you’ve lost any information about the .mat file. You can only access the variable s, which is the object itself.
That’s what I’m looking for, a way to figure out something about the .mat file which caused the method loadobj(s) of my MockObj class.
I hope it makes sense,
Nir G.

Sign in to comment.


Matt J
Matt J on 26 Aug 2020
Edited: Matt J on 26 Aug 2020
But why would this information be more useful to you in the loadobj workspace than in the workspace where you actually invoke the load() command? When the load command is invoked, you have the name of the .mat file and can do any addditional manipulations you want with it right then and there. If the code for these extra manipulations would be repetitive, then you can use an ordinary wrapper function or method to do the loading:
function S = load_custom(filename)
[pth,fname]=fileparts(which(filename));
FilePath=fullfile(pth,fname);
S = load(FilePath);
disp("The file contents were loaded from " + FilePath );
end
  4 Comments
Nir Gutman
Nir Gutman on 26 Aug 2020
Thanks for the reply.
I'm the one doing the saving, and I keep and know the location and name of the .mat file at the moment of saving.
Unfortunately, when the file is being moved to another folder and then being loaded, the information is no longer valid.
Any ideas?
Thanks,
Nir G.
Matt J
Matt J on 26 Aug 2020
My only idea, I'm afraid, is to submit an enhancement request to TMW to pass the source/destination matfile name to loadobj/saveobjas additional input arguments... I think it would open lots of useful possibilities!

Sign in to comment.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!