如何解决外部.m文件中的Matlab应用调用函数
我想在应用程序中调用getPhoto(),而getPhoto()是到同一目录中另一个.m文件的函数。
应用代码
jku
getPhoto()代码
properties (Access = public)
fullname % Description
ImageFile % Description
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: CaricaimmaginesRButton
function CaricaimmaginesRButtonPushed(app,event)
getPhoto();
test = app.fullname;
answer = questdlg('do you wanna crop?',...
'Dessert Menu',...
'Yes','No','No');
% Handle response
switch answer
case 'Yes'
app.ImageFile = imcrop(app.ImageFile);
close figure 1;
case 'No'
end
app.ImageFile = imresize(app.ImageFile,[300 200]);
app.TextArea.Visible = false;
app.UIAxes.Visible = true;
imshow(app.ImageFile,'Parent',app.UIAxes);
end
使用getPhoto()我想选择照片并将名称和照片传递给应用程序。
解决方法
您可以使用getPhoto函数仅返回文件名和图像,而无需修改应用程序数据结构。但是,如果您坚持要这样做,则必须修改代码:
% Change the callback to
app = getPhoto( app);
% Change the getPhoto function as below
function app = getPhoto( app)
cd('C:\Users\Gianl\Documents\MATLAB\app\test\Images');
[filename,filepath] = uigetfile({'*.*;*.jpg;*.png;*.bmp;*.oct'},'Select File to Open');
cd('C:\Users\Gianl\Documents\MATLAB\app');
app.fullname = [filepath,filename];
app.ImageFile = imread(app.fullname);
end
但是更好的解决方案是
% In the callback change getPhoto() to
[app.fullname,app.ImageFile] = getPhoto();
% Modify the getPhoto function as below
function [fullname,ImageFile] = getPhoto()
cd('C:\Users\Gianl\Documents\MATLAB\app\test\Images');
[filename,'Select File to Open');
cd('C:\Users\Gianl\Documents\MATLAB\app');
fullname = fullfile( filepath,filename);
ImageFile = imread(app.fullname);
end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。