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

조회 수: 30 (최근 30일)
Nick Gibson
Nick Gibson 2022년 5월 13일
댓글: dpb 2022년 5월 16일
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
Nick Gibson
Nick Gibson 2022년 5월 13일
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.
dpb
dpb 2022년 5월 13일
<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...

댓글을 달려면 로그인하십시오.

채택된 답변

Mike Croucher
Mike Croucher 2022년 5월 13일
편집: Mike Croucher 2022년 5월 13일
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
Mike Croucher
Mike Croucher 2022년 5월 16일
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 :)
dpb
dpb 2022년 5월 16일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2022년 5월 13일
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
Nick Gibson
Nick Gibson 2022년 5월 13일
thanks Jan, will check that out.
.
Walter Roberson
Walter Roberson 2022년 5월 13일
ctfroot is designed for this purpose.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 C Shared Library Integration에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by