What command could run any file.m chosen

Hi!
I create a GUI that allow me to browse files.m with the code below:
function start_Callback(hObject, eventdata, handles)
indir2 = uigetfile;
set(handles.edit1, 'String', indir2);
I get this result:
I want to run each time the selected file (regardless of the filename), but I still can not do it. How to proceed?
PS: I want to run the file via script.

 Accepted Answer

What about considering the file path:
function start_Callback(hObject, eventdata, handles)
[FileName, FilePath] = uigetfile('*.m');
if ischar(FileName) % Not aborted
File = fullfile(FilePath, FileName);
set(handles.edit1, 'String', File);
run(File);
end

2 Comments

Thanks, it works except for one m-file. This m-file contains just a variable set that must be displayed on workspace after its execution.
the file contains these variabls:
Pass = 0;
Coss = 1;
Tass = 0;
....
But after execution, nothing is displayed on the workspace.
Why do you assume that anything is displayed where?

Sign in to comment.

More Answers (1)

help run
FileName='c:\mydoc\test.m';
run(FileName);

6 Comments

I wrote this:
function start_Callback(hObject, eventdata, handles)
indir2 = uigetfile;
set(handles.edit1, 'String', indir2);
run(indir2);
I tested it but it does not work
"Does not work" is too lean to reconsider, what happens.
"it does not work" is an extremely unhelpful statement.
In what way does it not work? Does it not produce the result you expect? In which case what does it do and what did you expect? does it produce an error? In which case what is the full error message?
Note that if the file you want to run is not on the matlab path, you have to give its full path to run, so:
[indir2, inpath2] = uigetfile;
run(fullfile(inpath2, indir2));
thank you for your observations.
Actually, it works for all files except one. This file contains just a set of variables that must be displayed on the workspace after its execution.
Pass = 0;
Coss = 1;
Tass = 0;
....
But after execution, nothing is displayed on the workspace.
Well, that would be because the code actually does not tell matlab to display the variables. Either remove the semicolons to tell matlab you want to display the result of the assignments, or use disp or fprintf.
I find the solution:
function Browse_Calib_Callback(hObject, eventdata, handles)
indir2 = uigetfile;
set(handles.edit2, 'String', indir2)
run(indir2);
indir2 = indir2(1:end-2);
evalin('base', indir2);

Sign in to comment.

Categories

Find more on App Building in Help Center and File Exchange

Asked:

on 29 Nov 2016

Commented:

Jan
on 30 Nov 2016

Community Treasure Hunt

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

Start Hunting!