function in a GUI does not return the correct value after function's parameters have been modified outside of the GUI

Hi all, I have a GUI with external functions that I call inside callbacks of the GUI.
% define_Params() and compute_function() are written outside of the GUI
function params = define_Params();
params.value = 3;
end
% compute from compute_function
function outputdata = compute_function(inputdata, params)
params = define_Params();
outputdata = algo(inputdata, params ) ;
end
In my GUI, I have two callbacks:
1. function compute_pushbutton_Callback(hObject, eventdata, handles) to launch my computation
function apply_to_selectedImages_pushbutton_Callback(hObject, eventdata, handles)
params = define_Params();
outputdata = compute_function(inputdata, params) ;
% update
guidata(hObject, handles);
2. function loadParams_Callback(hObject, eventdata, handles) to load and hopefully to change the parameters for compute_function().
function loadParams_Callback(hObject, eventdata, handles)
...
% open define_Params(), this can be editable
fileattrib( [PathName, FileName],'+w' );
eval(['!"C:\Program Files (x86)\Notepad++\notepad++" ' PathName FileName])
......
% update
guidata(hObject, handles);
which opens define_Params() in Notepad++. And then, I change manually the value of params.value to 10, say, so in that function define_Params() becomes:
function [params] = define_Params()
params.value = 10;
end
I save in Notepad++, and re-launch my compute_pushbutton_Callback():
but the value of params.value is still equal to 3. in params though I changed it to 10. Also, I noticed, that sometimes, it is working and sometimes not ...
Can someone have some explanation about this, please ? And when I use debug mode, though the value is 10, it is still 3 ???
Thank you very much.

5 Comments

At a guess the function has been cached and does not realise it has changed.
Why would you ever want to have code that opens .m files for you to edit data in though???? You said you have a GUI so why wouldn't the values be supplied there if they are for the user to edit? Or in a .mat file which is the normal place to save data, not in a dynamic .m file.
You should call system rather than eval. system is the correct command for invoking system commands.
Soring data in .m files and editing them with Notepad++ sounds like a rather inefficient practice. Why not use standard GUI tools?
Hi, Thank you for the response but my question is not about eval() here, as the file is effectively opened with Notepad++ with eval(['!"C:\Program Files (x86)\Notepad++\notepad++" ' PathName FileName]). My question is about why the value are not updated after beeing modified.
I have to use Notepad++ since my application will be used with machines without Matlab (standalone application compiled with Matlab Compiler).
Adam, I will try .mat file after the response of Walter below. I have a lot of parameters to set, that's why I used a .m file and not in the GUI. At the time I wrote my algorithms, I didn't know yet that I will compile with Matlab Compiler. Thanks.

Sign in to comment.

 Accepted Answer

"I have to use Notepad++ since my application will be used with machines without Matlab (standalone application compiled with Matlab Compiler)."
Once a .m file has been compiled by MATLAB Compiler, it is "locked in" to the compiled executable, and changes to it will be ignored. The only way to change the behaviour of a .m that is compiled into an executable is to recompile the executable.
It is a deliberate part of the design of MATLAB Compiler that external .m cannot be evaluated at run-time. MATLAB Compiler is only for "closed" applications that do something specific. If a modifiable .m could be executed at run-time then it would be easy for someone to write a program that just executed an external .m file, compile that, and distribute that, and the effect would be to distribute free MATLAB licenses.

6 Comments

Thank you for your response. Would you have a solution to modify those parameters without .m file (I have a lot of parameters, so I can't use edit box to set them one by one) in my GUI. In my file parameters, I have several structs.
What's wrong with using an m-file? You certainly don't want to type all those lines of code in to the command window!!!
I want to modify the .m file (or the parameter file) in run-time while launching my app with the GUI, i.e. to be able to edit the values inside the parameter file without a lot of edit boxes as in a GUI.
As mentioned elsewhere, .m files are for code, .mat files (or .txt files or ascii or whatever file format you prefer that is meant to hold raw data) should contain parameters.
Thanks a lot, I will come back once I find the right solution for me. Best.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 15 Dec 2017

Commented:

on 18 Dec 2017

Community Treasure Hunt

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

Start Hunting!