Function to convert relative paths to absolute paths? - matlab

I've spent quite some time to no avail looking for a built-in MATLAB function to convert relative file paths to absolute file paths (portably).
Is there one?
I'm looking for something preferably "built-in" (i.e. available somewhere in the MATLAB distribution, including one of its toolboxes). Even a "package-private" function would be acceptable, as long as I can examine the source code of function. Second best would be a third-party function, as long as it comes with a decent test suite. I am not looking for a function written in response to this question.1
Absent any of the above, even a function to test (portably) whether a path is absolute or not would do (with the same conditions as before: either a "built-in" function or a third-party function with a test suite).
1 The difficulty with implementing such a function is not writing the function itself, but rather writing a sufficiently complete test suite for it (and, of course, making sure that the function passes all the tests!).

fullfile(pwd, relative_path) converts a relative to a absolute path.

You can test if a path is absolute using
javaFileObj = java.io.File(pathToBeTested);
javaFileObj.isAbsolute()
Unlike char(javaFileObj.getCanonicalPath()), which indeed sometimes incorrectly returns a non-existent path relative to C:\Windows\System32, isAbsolute() seems to work properly (tested on Win7, MATLAB 2015b) Therefore the code for constructing the absolute path would look like
function F = rel2abs(F)
if ~java.io.File(F).isAbsolute
F = fullfile(pwd,F);
end
This function has the advantage of being idempotent.

The fullfile(pwd, relative_path) hack works well for me, but if you want something to get you the canonical form, there is no built-in (as of 2015b), but there is a well regarded downloadable script.
http://www.mathworks.com/matlabcentral/fileexchange/28249-getfullpath

See if which fulfills your requirements:
full_path = which(relative_path);

OK, let's resurrect an old thread, if anyone is looking for this.
Here is a method, if you want the absolute path relative to your current working directory (or pwd).
% pwd: 'C:\first\branch'
dir('.\').folder % returns same as pwd, 'C:\first\branch'
dir('..\').folder % returns path one level up from pwd, 'C:\first'
dir('..\parallel_branch').folder % returns the absolute path to "parallel_branch" folder next to pwd, 'C:\first\parallel_branch'

Here's the method that MathWorks themselves uses:
[status, info] = fileattrib(file);
if status
% Return the full path if fileattrib found the file.
fullFilePath = info.Name;
end

For someone landing here with slightly relaxed portability requirements (or a suitable test bench), another idea is to create a folder listing containing only a single file using the built-in dir function, followed by path concatenation using fullfile.
dirListing = dir(relPath);
absPath = fullfile(dirListing(1).folder, dirListing(1).name);
Pros:
Idempotent, i.e. rel2abs(rel2abs(path)) = rel2abs(path)
Simplifies out any /. and /.. in the relative path
Cons:
This only works if the file exists
Tested using Matlab R2020a on Windows

Related

Matfile isn't loading the structure's field names

Background
I encountered some strange behaviour with the function "matfile" in Matlab 2016b - not sure what's going on, and I can't replicate it or create a test case.
I have a structure, which I saved to a server, like so:
PathToFile='ServerPath\My Documents\MyProj\testMatF.mat';
save(PathToFile,'-struct','myStruct'); %I tried the -v7.3 flag
Problem
Then I read it in with:
m1=matfile(PathToFile);
On other, very similar structs, I can do:
myFields=fieldnames(m1);
But for this one file I can't, all I get is the auto "Properties" field.
What I've tried
myFields=who(m1) - gives me list of fieldnames... sometimes. I don't know the who function well, but it seems, if I intersperse who m1 then myFields=who(m1) it works.
Explicitly typing m1.TheFieldName, works.
Moving the file to a location on the comp, like C:\Data\. Then using fieldnames works.
Using load, works.
Does it have to do with the server access, corrupted file, matfile properties? One other weird thing is some of my .m files in this particular folder, when I try to open them results in: Does not exist, when clearly it does, since I click on it and can use the run function with it... Additional: Windows 7. Recently updated license.
Please let me know what info you can use to help out. Either to create a new file that will work, or fix the problem with the current file. Thanks.
EDIT
Example output in my command window - seemingly incomprehensible...
m1=matfile(fullfile(projPath,'NexusMarkersProcessed.mat'),'Writable',false)
m1 =
matlab.io.MatFile
Properties:
Properties.Source: '\bontempi.medicine.utorad.utoronto.ca\home\PT\zabjeklab3\My
Documents\Data\Active Projects\JC_Hip\FL1502\FL1502\Patient
Classification 2\NexusMarkersProcessed.mat'
Properties.Writable: false
Methods
K>> m1.Properties.Source
ans =
\bontempi.medicine.utorad.utoronto.ca\home\PT\zabjeklab3\My
Documents\Data\Active Projects\JC_Hip\FL1502\FL1502\Patient
Classification 2\NexusMarkersProcessed.mat
K>> java.io.File(m1.Properties.Source).exists()
ans =
logical
0
Pause to paste in this window... go back:
java.io.File(m1.Properties.Source).exists()
ans =
logical
1
K>> who(m1)
Your variables are:
Patient10 Patient5 Patient9 Patient11 Patient6 Patient3
Patient7
K>> who(m1) K>> who(m1) K>>
java.io.File(m1.Properties.Source).exists()
ans =
logical
0
K>>
So it sometimes finds the file, and can read it in. Othertimes it cannot - is this to do with the fact that it's on a network drive?
Any help is appreciated.
This issue is caused by accessing a file on a network drive. One workaround is to copy the file to a local drive (C: is used), and then use matfile, use the result as needed, then replace the network drive file, and delete the local file, to return things to their original state. Some research made me realize things are slower than they need to be if any files, even the .m ones, are on the network. Here's a function that worked:
function [m,noData]=safeMatFile(FilePath,safeFilePath)
%FilePath: absolute path to where the file is on the network
%safeFilePath: absolute path to where the file will be temporarily copied locally, C://
%safeDir='C:\Data';
%safeFold='tempFolder12345679';
%[~,tempDir,~]=mkdir(safeDir,safeFold);
%safeFilePath=fullfile(safeDir,safeFold,FileName);
noData=0;
dirFile=dir(FilePath);
if (length(dirFile) == 1) && ~(dirFile.isdir)%OR java below OR exist(forceFilePath,'file')==2
%if there is a file, make a temp folder on the C drive
if ~(java.io.File(safeFilePath).exists() && java.io.File(safeFilePath).isFile()) %ensure file doesn't exist, check dir too: || isempty(tempFolder)
copyfile(FilePath, safeFilePath);%moves existing file to backup folder
else
warning('SKIPPING (%s) - an old file of the same name was there, delete it!\n',safeFilePath);
return
end
%Load the temp local file into matlab
m=matfile(safeFilePath,'Writable',true);
else
m=[];
noData=1;
end
end
Then do stuff with m... and at the end:
function overwriteOldFiles()
if (java.io.File(safeFilePath).exists() && java.io.File(safeFilePath).isFile())
java.io.File(FilePath).delete();
java.io.File(safeFilePath).renameTo(java.io.File(FilePath));
end
end
and
function deleteTempFiles()
if (java.io.File(safeFilePath).exists() && java.io.File(safeFilePath).isFile())
java.io.File(safeFilePath).delete();
end
end
... Then rmdir if necessary.
Note, I tried different ways of checking if the file exists (I think the first is fastest and most reliable):
dirFile=dir(FilePath); if (length(dirFile) == 1) && ~(dirFile.isdir)
if (java.io.File(FilePath).exists() && java.io.File(FilePath).isFile())
if exist(FilePath,'file')==2

Referencing External Files in JModelica

I have a Modelica file that references c code during simulation through an external library *.a file.
For example:
model CallAdd
input Real FirstInput(start=0);
input Real SecondInput(start=0);
output Real FMUOutput(start=0);
function CAdd
input Real x(start=0);
input Real y(start=0);
output Real z(start=0);
external "C" annotation(Library = "CAdd", LibraryDirectory = "modelica://CallAdd");
end CAdd;
equation
FMUOutput = CAdd(FirstInput,SecondInput);
annotation(uses(Modelica(version = "3.2.1")));
end CallAdd;
When opening the Modelica model in OpenModelica the required files appear to be automatically loaded because it simulates and gives appropriate results.
However, when I try to compile the Modelica file with JModelica-SDK-1.12 I receive an error that the library *.a file could not be found.
So my question is: What is the proper way to reference additional files when using compile_fmu in JModelica?
With no success, I've tried:
# Import the compiler function
from pymodelica import compile_fmu
model_name = "CallAdd"
mo_file = "CallAdd.mo"
# Compile the model and save the return argument, for use later if wanted
my_fmu = compile_fmu(model_name, mo_file, target="cs",compiler_options = {'extra_lib_dirs':'C:/ToFolderContainingLib/'})
The strange thing is that when I was using JModelica-1.17 (non-SDK) the file compiled fine but the results didn't make sense. I was recommended to try the SDK version to see if it fixed my errors in my previous post here.
Try positioning the external library in sub-folder named as the platform your currently on. So in your example, I'd position the library (libCAdd.a) in sub-folder named linux64, as I'm on a 64bit Linux machine and then run the code.
If is a small piece of C code, as a last alternative you could try to include the C file directly in the Modelica code:
external "C" annotation(Include="
// the entire C code here
");
Hopefully the JModelica people will give you a better answer soon.
You could try to ask this on their website also:
http://www.jmodelica.org/forum

How can I make sure that Matlab mcc doesn't build incomplete stand-alone executables?

we use a scripting environment to automatically build, run and verify a stand-alone executable. We are working with Matlab R2010a x64. The Matlab compiler mcc is called from the Windows command line building a stand-alone application:
mcc -m -v -w enable -I source_folder -I common_folder -a specific_files_we_need our_program
The program consists of about 25 modules (.m files) and is using about 5 toolboxes. This is working fine as long as the correct license is available. mcc checks for a compiler license available, resolves dependencies, packs everything in the executable.
However if the license does not include the required toolboxes, mcc does not issue any warning or error. It builds the executable without the toolboxes. So the executable starts, seems to run at a first glance but crashes if a line of code requiring a toolbox is reached.
I am expecting from a compiler that it informs me about missing components. What can I do to get informed about missing components? How can I make sure that mcc does not put together incomplete executables? Am I missing something in the call to mcc?
Preferably I would like to setup the compiling in a way that it stops if things are missing.
\Zweikeks
The simplest way is in your compilation script you can checkout the licenses required, i.e.
license('checkout','Compiler')
license('checkout','control_toolbox')
You just add the 5 toolboxes that need to be checked out -> if the license function cant checkout the license it returns false which you can then use to abort the compilation.
This is what I finally came up with:
I can check before compilation which toolboxes are needed and call license() accordingly. Or I can implement a built-in check into the executable itself. (Called with a special parameter after compilation triggers a self-check for available toolboxes.) In either case I need the names of the required toolboxes.
I tried several ways to generate a list of toolboxes. In short: Running the program in Matlab and then entering license('inuse') is not very reliable. depfun() descends way to much. mydepfun() and fdep() do not descend enough.
I think the problem with mydepfun() and fdep() is that they do not descend into the \toolbox\shared folder. So I took mydepfun() from Tobias Kienzler (link to the original sources) and modified it:
function [list,callers,tboxes_found] = i_scan(f)
func = i_function_name(f);
[list,~,~,~,~,~,callers,~] = depfun(func,'-toponly','-quiet');
toolboxroot = fullfile(matlabroot,'toolbox');
sharedroot = strcat(toolboxroot, filesep, 'shared');
intoolbox = strncmpi(list,toolboxroot,numel(toolboxroot));
inshared = strncmpi(list,sharedroot, numel(sharedroot));
tboxes_found = list(intoolbox & ~inshared);
tboxes_found = regexpi(tboxes_found, '[\\/]toolbox[\\/](.+?)[\\/]', 'tokens');
tboxes_found = cellfun(#(cfun) cfun{1}, tboxes_found);
list = list(~intoolbox | inshared);
callers = callers(~intoolbox | inshared);
for jj = 1:numel(list)
c = callers{jj};
cs = cell(numel(c),1);
for kk = 1:numel(c)
cs{kk} = list{c(kk)};
end;
callers{jj} = cs;
end;
This way i_scan(f) is returning the toolboxes and also descends into \toolbox\shared. The main function of mydepfun() just collects the toolboxes:
function [filelist,callers,toolboxes] = mydepfun(fn,recursive)
.
.
toolboxes = {};
[filelist,callers,tboxes_found] = i_scan(foundfile);
toolboxes = [toolboxes; tboxes_found];
.
.
[newlist,newcallers,tboxes_found] = i_scan(toscan{1});
toolboxes = [toolboxes; tboxes_found];
.
.
toolboxes = unique(toolboxes);
The toolboxes listed are the ones our source code uses. The modified mydepfun() seems to work fine. (Apart from the typical problems caused by elements only resolved during run time like eval(), function handles, callbacks etc.)
And: The dependeny walkers I have seen - like mydepfun() - are using depfun() inside. depfun() is not reliable since it silently ignores all source code not on the path (also its return prob_files is empty in this case). So care has to be taken that the Matlab path is set correctly. (Also any additional pathes are problematic since Matlab may take unexpectedly functions with the same name from other locations.)
After all, I think, this is a good way to make my build process more reliable.
/Zweikeks
I just got another hint from the Mathworks forum. The compiler writes out mccExludedFiles.log. This is listing missing toolboxes.
For example
mccExludedFiles.log:
C:\Program Files\MATLAB\R2010a\toolbox\shared\optimlib\fmincon.m
called by ...c:\temp\whatever\source\code.m
(because the required licenses are not available.)
(Other missing files in the source code do not get listed, though.)
/Zweikeks

What is the full command for gdal_calc in ipython?

I've been trying to use raster calculation in ipython for a tif file I have uploaded, but I'm unable to find the whole code for the function. I keep finding examples such as below, but am unsure how to use this.
gdal_calc.py -A input.tif --outfile=result.tif --calc="A*(A>0)" --NoDataValue=0
I then tried another process by assigning sections, however this still doesn't work (code below)
a = '/iPythonData/cstone/prec_7.tif'
outfile = '/iPythonData/cstone/prec_result.tif'
expr = 'A<125'
gdal_calc.py -A=a --outfile=outfile --calc='expr' --NoDataValue=0
It keeps coming up with can't assign to operator. Can someone please help with the whole code.
Looking at the source code for gdal_calc.py, the file is only about 300 lines. Here is a link to that file.
https://raw.githubusercontent.com/OSGeo/gdal/trunk/gdal/swig/python/scripts/gdal_calc.py
The punchline is that they just create an OptionParser object in main and pass it to the doit() method (Line 63). You could generate the same OptionParser instance based on the same arguments you pass to it via the command-line and call their doit method directly.
That said, a system call is perfectly valid per #thomas-k. This is only if you really want to stay in the Python environment.

MATLAB: force doc command to open a specified reference HTML page

Say I've written a class in a package, called mypackage.myclass. I've written my own HTML documentation for the package and the class, and have included this within the MATLAB help browser as described in the MATLAB documentation.
I can display this HTML documentation by using the help browser to navigate directly to it, but typing doc mypackage.myclass does not display it; instead it displays some HTML documentation that is auto-generated by helpwin (which is a nice feature, but not what I want - the auto-generated documentation is too techy for my users).
How can I force doc to display my documentation, rather than the auto-generated documentation?
Equivalently:
When you run doc docTopic, inside the doc command the Java class com.mathworks.mlservices.MLHelpServices.showReferencePage(docTopic) gets called. If a reference page for docTopic exists, it displays it and returns a success value. If a reference page doesn't exist, it returns a failure value, which then causes helpwin(docTopic) to get called. Somewhere there must be some catalog that connects values of docTopic with individual reference HTML files. How can I fiddle with that catalog - or can I create one for my package?
MathWorkers and #Yair, please give me enough undocumented rope to hang myself with :)
As far as I know this is not possible and not intended by MathWorks. I don't know of an undocumented way of doing this either. As far as I remember the keywords for doc are hard-coded somewhere.
Depending on your setup you can try the following: Prepare your own doc command that uses web(..., '-helpbrowser') to display HTML pages in MATLAB's help browser:
function doc(topic)
my_topics = {
'foo', 'foo.html'
'bar', 'bar/help/intro.html'
};
for i = 1 : size(my_topics, 1)
if strcmpi(topic, my_topics{i, 1})
web(my_topics{i, 2}, '-helpbrowser');
return;
end
end
% Fall back to MATLAB's doc. Note that our doc shadows MATLAB's doc.
docs = which('doc', '-all');
old_dir = cd();
c = onCleanup(#() cd(old_dir));
cd(fileparts(docs{2}));
doc(topic);
end
If you put that function in a file doc.m and put the corresponding directory at the beginning of the MATLAB path (see help addpath) then it will be called instead of the built-in doc.
Of course you could use some other place to store your custom doc mapping (a file, for instance) or use some kind of dynamic lookup scheme.
UPDATE: As of MATLAB R2012b, the '-helpbrowser' option of web is undocumented. This is probably related to the GUI changes in that MATLAB version, which also include the help browser. web(..., '-helpbrowser') still works as intended, but that may change in future versions of MATLAB. As far as I know, there is no documented way of opening any HTML page in the help browser in R2012b.