Using pushbutton to load an .m file in GUI

조회 수: 4 (최근 30일)
Laila Buncab
Laila Buncab 2015년 5월 27일
댓글: Walter Roberson 2015년 5월 27일
I'd like for a pushbutton to open a .m file(when you run the .m file, a .fig file appears). How do I do that?
I actually don't know anything about MATLAB, this was just my officemate's work passed on to me.

채택된 답변

Joseph Cheng
Joseph Cheng 2015년 5월 27일
you would first create a pushbutton in either GUIDE or manually using uicontrol. Then you would change the callback function to run a function that uses uigetfile() and the run() functions. see my example:
function test() %main function
%create the button and adjust the callback
btn = uicontrol('Style', 'pushbutton', 'String', 'Clear',...
'Position', [20 20 50 20],...
'Callback', @runfile);
%what the button does when clicked
function runfile(hobject,event)
[file folder]=uigetfile('*.m'); %prompt user to select mfile
run(fullfile(folder,file)); %fun the m-file.
  댓글 수: 4
Laila Buncab
Laila Buncab 2015년 5월 27일
It does run but when I click the button, a clear button appears at the bottom left corner.
I clicked the clear button, and proceeded to open 'performancegraph.m', but this error appears in the command window.
Warning: Function C:\Users\USER\Documents\OJT-orig -
Copy\home\home.m has the same name as a MATLAB
builtin. We suggest you rename the function to avoid a
potential name conflict.
> In run at 39
In home>runfile at 84
Error using run (line 40)
C:\Users\USER\Documents\OJT-orig-Copy\home\performancegraph.m
not found.
Error in home>runfile (line 84)
run('C:\Users\USER\Documents\OJT-orig-Copy\home\performancegraph.m');
Error while evaluating uicontrol Callback
Walter Roberson
Walter Roberson 2015년 5월 27일
If you want the button in a different place or different size then change the parameter you are passing for Position.
The Warning is a warning and does not affect what comes after.
run() is able to find the directory you specified. However, there is no performancegraph.m there. Check for spelling mistakes, and check in case the actual file name has uppercase in some characters.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by