Skip to content
This repository has been archived by the owner on Nov 25, 2018. It is now read-only.

Maybe I'm missing something? Conversion issue #9

Open
ksingh49 opened this issue Apr 3, 2017 · 1 comment
Open

Maybe I'm missing something? Conversion issue #9

ksingh49 opened this issue Apr 3, 2017 · 1 comment
Assignees

Comments

@ksingh49
Copy link

ksingh49 commented Apr 3, 2017

Hey,

Please excuse me if this has a really easy solution but I'm farily new to matlab and coding in general.

So I've been using mlapp2classdef to convert a simple app I made in App Designer just to test if I can end up with a final standalone file that works. The sample is just an axes with a push button that plots cos(x) after it is pushed. After I run the conversion, mlapp2classdef([], 'ReplaceAppUI, true) I get an error saying:

While setting property 'UIAxes' of class 'testclick': Value must be 'matlab.ui.control.UIAxes'. Error in testclick/createComponents (line 39) app.UIAxes = axes(app.UIFigure); Error in testclick (line 79) createComponents(app)

My file name is "testclick". Here is the code that the conversion generates from my sample.

classdef testclick < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure@matlab.ui.Figure
        UIAxes@matlab.ui.control.UIAxes
        Plot@matlab.ui.control.Button
        XEditFieldLabel@matlab.ui.control.Label
        XEditField@matlab.ui.control.NumericEditField
        YEditFieldLabel@matlab.ui.control.Label
        YEditField@matlab.ui.control.NumericEditField
    end

    methods (Access = private)

        % Button pushed function: Plot
        function PlotPushed(app, event)
            x=0:.01:100;
            y=cos(x);
            plot(app.UIAxes,x,y);
            app.XEditField.Value=10;
            app.YEditField.Value=100;
        end
    end

    % App initialization and construction
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure
            app.UIFigure = figure;
            app.UIFigure.Position = [100 100 634 308];
            app.UIFigure.Name = 'UI Figure';
            setAutoResize(app, app.UIFigure, true)

            % Create UIAxes
            app.UIAxes = axes(app.UIFigure);
            title(app.UIAxes, 'Title');
            xlabel(app.UIAxes, 'X');
            ylabel(app.UIAxes, 'Y');
            app.UIAxes.Position = [259 80 300 185];

            % Create Plot
            app.Plot = uicontrol('Parent', app.UIFigure, 'push', 'Style', 'pushbutton');
            app.Plot.ButtonPushedFcn = createCallbackFcn(app, @PlotPushed, true);
            app.Plot.Position = [96 216 100 22];
            app.Plot.Text = 'Plot';

            % Create XEditFieldLabel
            app.XEditFieldLabel = uicontrol('Parent', app.UIFigure, 'Style', 'text');
            app.XEditFieldLabel.HorizontalAlignment = 'right';
            app.XEditFieldLabel.Position = [56 165 25 15];
            app.XEditFieldLabel.Text = 'X';

            % Create XEditField
            app.XEditField = uicontrol('Parent', app.UIFigure, 'numeric', 'Style', 'edit');
            app.XEditField.Position = [96 161 100 22];

            % Create YEditFieldLabel
            app.YEditFieldLabel = uicontrol('Parent', app.UIFigure, 'Style', 'text');
            app.YEditFieldLabel.HorizontalAlignment = 'right';
            app.YEditFieldLabel.Position = [57 118 25 15];
            app.YEditFieldLabel.Text = 'Y';

            % Create YEditField
            app.YEditField = uicontrol('Parent', app.UIFigure, 'numeric', 'Style', 'edit');
            app.YEditField.Position = [97 114 100 22];
        end
    end

    methods (Access = public)

        % Construct app
        function app = testclick()

            % Create and configure components
            createComponents(app)

            % Register the app with App Designer
            % Function call removed

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end

And lastly here is the code converted just with mlapp2classdef([])

classdef testclick < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure         matlab.ui.Figure
        UIAxes           matlab.ui.control.UIAxes
        Plot             matlab.ui.control.Button
        XEditFieldLabel  matlab.ui.control.Label
        XEditField       matlab.ui.control.NumericEditField
        YEditFieldLabel  matlab.ui.control.Label
        YEditField       matlab.ui.control.NumericEditField
    end

    methods (Access = private)

        % Button pushed function: Plot
        function PlotPushed(app, event)
            x=0:.01:100;
            y=cos(x);
            plot(app.UIAxes,x,y);
            app.XEditField.Value=10;
            app.YEditField.Value=100;
        end
    end

    % App initialization and construction
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure
            app.UIFigure = uifigure;
            app.UIFigure.Position = [100 100 634 308];
            app.UIFigure.Name = 'UI Figure';
            setAutoResize(app, app.UIFigure, true)

            % Create UIAxes
            app.UIAxes = uiaxes(app.UIFigure);
            title(app.UIAxes, 'Title');
            xlabel(app.UIAxes, 'X');
            ylabel(app.UIAxes, 'Y');
            app.UIAxes.Position = [259 80 300 185];

            % Create Plot
            app.Plot = uibutton(app.UIFigure, 'push');
            app.Plot.ButtonPushedFcn = createCallbackFcn(app, @PlotPushed, true);
            app.Plot.Position = [96 216 100 22];
            app.Plot.Text = 'Plot';

            % Create XEditFieldLabel
            app.XEditFieldLabel = uilabel(app.UIFigure);
            app.XEditFieldLabel.HorizontalAlignment = 'right';
            app.XEditFieldLabel.Position = [56 165 25 15];
            app.XEditFieldLabel.Text = 'X';

            % Create XEditField
            app.XEditField = uieditfield(app.UIFigure, 'numeric');
            app.XEditField.Position = [96 161 100 22];

            % Create YEditFieldLabel
            app.YEditFieldLabel = uilabel(app.UIFigure);
            app.YEditFieldLabel.HorizontalAlignment = 'right';
            app.YEditFieldLabel.Position = [57 118 25 15];
            app.YEditFieldLabel.Text = 'Y';

            % Create YEditField
            app.YEditField = uieditfield(app.UIFigure, 'numeric');
            app.YEditField.Position = [97 114 100 22];
        end
    end

    methods (Access = public)

        % Construct app
        function app = testclick()

            % Create and configure components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end
@sco1
Copy link
Member

sco1 commented Apr 4, 2017

This is probably related to MATLAB using both uiaxes and UIAxes in its GUI class definition, the regular expression dictionary built for mlapp2classdef is case sensitive, so only uiaxes is being replaced with axes and UIAxes is left alone.

Fixing that, however, blows up into a lot of other issues, most of which related to the regular expressions being too greedy and also a lot of differences between the new UI components and the old ones that I didn't account for. I'll poke around and implement some fixes as I have time.

Here's a fixed version of your code:

classdef testclick

    % Properties that correspond to app components
    properties (Access = public)
        mainfigure
        mainaxes
        plotbutton
        XEditFieldLabel
        XEditField
        YEditFieldLabel
        YEditField
    end

    methods (Access = private)

        % Button pushed function: plotbutton
        function plotbuttonPushed(app, event)
            x=0:.01:100;
            y=cos(x);
            
            hold(app.mainaxes, 'on');
            plot(app.mainaxes,x,y);
            hold(app.mainaxes, 'off');
            
            app.XEditField.String = 10;
            app.YEditField.String = 100;
        end
    end

    % App initialization and construction
    methods (Access = private)

        % Create mainfigure and components
        function createComponents(app)

            % Create mainfigure
            app.mainfigure = figure;
            app.mainfigure.Position = [100 100 634 308];
            app.mainfigure.Name = 'UI Figure';

            % Create mainaxes
            app.mainaxes = axes(app.mainfigure);
            title(app.mainaxes, 'Title');
            xlabel(app.mainaxes, 'X');
            ylabel(app.mainaxes, 'Y');
            app.mainaxes.Units = 'pixels';
            app.mainaxes.Position = [259 80 300 185];

            % Create plotbutton
            app.plotbutton = uicontrol('Parent', app.mainfigure, 'Style', 'pushbutton');
            app.plotbutton.Callback = @(src, event)app.plotbuttonPushed(event);
            app.plotbutton.Position = [96 216 100 22];
            app.plotbutton.String = 'plotbutton';

            % Create XEditFieldLabel
            app.XEditFieldLabel = uicontrol('Parent', app.mainfigure, 'Style', 'text');
            app.XEditFieldLabel.HorizontalAlignment = 'right';
            app.XEditFieldLabel.Position = [56 165 25 15];
            app.XEditFieldLabel.String = 'X';

            % Create XEditField
            app.XEditField = uicontrol('Parent', app.mainfigure, 'Style', 'edit');
            app.XEditField.Position = [96 161 100 22];

            % Create YEditFieldLabel
            app.YEditFieldLabel = uicontrol('Parent', app.mainfigure, 'Style', 'text');
            app.YEditFieldLabel.HorizontalAlignment = 'right';
            app.YEditFieldLabel.Position = [57 118 25 15];
            app.YEditFieldLabel.String = 'Y';

            % Create YEditField
            app.YEditField = uicontrol('Parent', app.mainfigure, 'Style', 'edit');
            app.YEditField.Position = [97 114 100 22];
        end
    end

    methods (Access = public)

        % Construct app
        function app = testclick()

            % Create and configure components
            createComponents(app)

            % Register the app with App Designer
            % Function call removed
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete mainfigure when app is deleted
            delete(app.mainfigure)
        end
    end
end

I got rid of the class enforcement of the properties because it's not really necessary for this case. And it was annoying me.

@sco1 sco1 self-assigned this Apr 4, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants