using matlab compiler to read a .mat file at run time

I want to compile a. m file, which included a load statement to load a .mat file, normally it seems this .mat file is then included in the compiled .exe file and cannot change (without recompilation). However, I would like the .exe file to load the .mat file at run time so I can change the contents of the .mat file with new data values. How do I do this?

7 Comments

How do you select the file currently in the m-file? What about a file choosing dialog?
thanks for your response, but I just want the same file to be loaded every time, just a .mat file in the same directory as the .exe file.
just the line:
load('data1.mat')
in the .m file to load it.
The users would find it unnecessary and annoying to have to select the file form a dialog every time!
I just need to be able to change the content of data1.mat every now and again.
if I change it to:
file=uigetfile
load('data1.mat')
sure, it reads the file each time
but I do not want the user to have to browse for the file.
I also tried:
file=data1.mat
load(file)
it still seems to blow the file itself into the compiled .exe file and if I change the content it does not read the new content when I run it.
Would need to see more on this -- my compiled (standalone) app loads and writes a .mat file dynamically with no issues -- the load command is in a function called from the app startupFcn(app) function that is automagically called. In this case, the .mat file contains only "behind-the-scenes" user settings for the app for restoration of state the next time is called so it is updated/rewritten only when the app is closed so the save() command is in the app close for ExitMenu,QuitButton, but a data file could be loaded/saved at will.
IOW, I don't see any such behavior with a standalone compiled app -- this was built with App Designer so has all its startup/shutdown code and all. I've not tried building something completely programmatically.
thanks dpb.
OK, so with a bit more experimenting I thik it's not finding the .mat file when I do:
file=data1.mat
load(file)
When I hard code the full path it works, like:
file='c:\tempdir\data1.mat'
load(file)
that's sort of OK, but ideally I want it to find the .mat file in the directory where the .exe resides, which may be different for different users.
That's going to be dependent upon how the install is set up -- the above app also just references the file name and the installer seems to work to put the file where it needs to be.
Do you show it in the dependencies? I forget just now whether there's a way to specify the file location during the build process or not; don't recall that I had to do anything specific other than ensure it showed the dependency.
Been too long since built it the first time to remember, details precisely, otomh...
I think I do recall there being a discussion topic in the documentation on locating data files, though...dig through the app developer doc's.
OK, so I think I've solved this by using pwd:
file=[pwd '\data1.mat']
load(file)
works for picking the .mat file up from the directory the .exe resides in.
thanks for your pointers.
<matlab-data-file-mat-files> is example load/save with deployed .mat files was remembering ... up one level from there is all you could want to know including the link @Jan shows...

Sign in to comment.

 Accepted Answer

You can tell the compiler to exclude things using an Exclude pragma A demo:
I created a .mat file as follows
myvar = 1;
save('data1.mat')
Then create this code which I call loadAndShow.m
%#exclude data1.mat
file = "data1.mat";
load(file)
fprintf("The value of myvar is %d\n",myvar)
Compile with
mcc -m loadAndShow.m
It will load the data1.mat file from the same directory as the .exe at runtime. Convince yourself of this by changing the contents of data1.mat.

3 Comments

thanks Mike, this is exactly what I was looking for. It sure works, and prefer to the pwd method for the potential robustness issues stated above.
(just shame it looks like a comment; a little confusing, I have to put a comment in to remind me it's not!)
I can see why it can be confusing but here's another angle to look at it:
For 'normal' MATLAB, the exclude pragma doesn't mean anything! It is solely an instruction to the compiler -- MATLAB itself can (and should!) ignore it. The easiest way to get MATLAB to ignore it is to present it as a comment.
Another example is the %#OK Pragma that tells the code analyser to stay quiet. It doesn't affect how MATLAB runs the code in any way but it is interpreted by the code analyser.
MathWorks could have chosen a different symbol for denoting such things I guess. You've made me curious about the decision to go with the %# syntax now :)
The # is the C Standard pragma indicator; adding the % comment character mimics what other compiler vendors have done for other languages. !DEC for the (at one time nearly ubiquitous) Digital Equipment Fortran compiler -- "!" is the comment character for freeform source. One can also use cDEC! or dDEC! where the c(C) or d(D) are the fixed form comment -- D being recognized as the debug lines (something TMW hasn't implemented with MATLAB -- we're still forced to stick with physically commenting/uncomment out lines.

Sign in to comment.

More Answers (1)

Consider, that pwd is fragile. Any subfunction might change the current directory.
See here for a method to get the location of the exe file:
The ctfroot command might be useful also.

2 Comments

thanks Jan, will check that out.
.
ctfroot is designed for this purpose.

Sign in to comment.

Categories

Products

Release

R2020b

Tags

Asked:

on 13 May 2022

Commented:

dpb
on 16 May 2022

Community Treasure Hunt

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

Start Hunting!