外部.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