MATLAB Answers

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

11 views (last 30 days)
Nir Gutman
Nir Gutman on 18 Aug 2020
Commented: Matt J on 26 Aug 2020
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.

  0 Comments

Sign in to comment.

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

Show 3 older comments
per isakson
per isakson on 21 Aug 2020
Your description assumes a context, which I don't share. I need more details. My understanding
  • "mock_obj" is not a file, it's a variable. Do you mean "mock_obj.mat"?
  • "moved the file manually" Does "file" refer to "mock_obj.mat"?
  • "I've loaded the file from it's new location" What exactly did you do?
A literal interpretation of what you did
>> load_mock_obj
The obj was loaded from D:\m\cssm\load_mock_obj.m
I moved mock_obj.mat to another folder, which is on the search path
>> load_mock_obj
The obj was loaded from D:\m\cssm\load_mock_obj.m
>>
Discussion:
  • "% The obj was loaded from C:\second". My solution did never solve your problem: "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 misinterpreted this sentence.
  • "Unfortunately, it works only when I run it as one script" This fooled me. I would say it never works. You ask for a folder and my "solution" returns a full file specification.
  • I failed to reproduce what you did.
  • I'm confused (at a higher level)! Are these two sentences of your question consistent? When loading a saved file containing an instance of MockObj, I would like to print the location of this file, from within the method." and "which will give me the fullpath of the file which caused MockObj to load." I would say no, they are not consistent. My solution aimed at the objective of the second sentence.
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

Show 1 older comment
Matt J
Matt J on 26 Aug 2020
Are you the one doing the saving? The only thing I can think of is at the time of saving you write the .mat file name to a class property...
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!