Matlab app call function in external .m file

1k Views Asked by At

I want call getPhoto() into the app and getPhoto() is a function into another .m file in same directory.

App code

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() code

function getPhoto()
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 = app2;
 app.fullname = [filepath,filename];
 app.ImageFile = imread(app.fullname);
 end

with getPhoto() i want to choose the photo and pass to the app the name and the photo into the properties of the app.

1

There are 1 best solutions below

0
On BEST ANSWER

You could use getPhoto function to return just the filename and image, without modifying the app data structure. But if you insist of doing so, you have to modify your code:

% 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

But a better solution would be

% 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,filepath] = uigetfile({'*.*;*.jpg;*.png;*.bmp;*.oct'}, 'Select File to Open');

    cd('C:\Users\Gianl\Documents\MATLAB\app');

     fullname = fullfile( filepath, filename);
     ImageFile = imread(app.fullname);
 end