using matlab compiler to read a .mat file at run time
20 views (last 30 days)
Show older comments
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
dpb
on 13 May 2022
<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...
Accepted Answer
Mike Croucher
on 13 May 2022
Edited: Mike Croucher
on 13 May 2022
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
dpb
on 16 May 2022
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.
More Answers (1)
Jan
on 13 May 2022
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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!