Untitled

mail@pastecode.io avatarunknown
plain_text
22 days ago
2.9 kB
1
Indexable
Never
classdef app1 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure  matlab.ui.Figure
        UIAxes    matlab.ui.control.UIAxes
    end

    % Callbacks that handle component events
    methods (Access = private)

        % Key press function: UIFigure
        function ReceiveDataButtonPushed(app, event)
            key = event.Key;
            serialportlist("available")
            
            s = serialport("COM5", 9600); 
            
            configureTerminator(s, "LF"); 
            flush(s); 
            pause(2); 
     
            receivedData = [];

            second = 0;
          
            while true
                data = readline(s); 
                data = str2double(data); 
                receivedData = [receivedData, data];

                second = second + 1;

                cla(app.UIAxes); 
                plot(app.UIAxes, 1:second, receivedData, 'r-');

                xticks(app.UIAxes, 1:second);  
                xticklabels(app.UIAxes, num2str((1:second)'));  

                ylim(app.UIAxes, [0, 1]);

                drawnow; 

                fprintf("Received: %f\n", data);
                fprintf("now: %d\n", second);
            end
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 640 480];
            app.UIFigure.Name = 'MATLAB App';
            app.UIFigure.KeyPressFcn = createCallbackFcn(app, @ReceiveDataButtonPushed, true);

            % Create UIAxes
            app.UIAxes = uiaxes(app.UIFigure);
            title(app.UIAxes, 'Arduino Due data')
            xlabel(app.UIAxes, 'time')
            ylabel(app.UIAxes, 'data')
            zlabel(app.UIAxes, 'Z')
            app.UIAxes.XTick = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1];
            app.UIAxes.Position = [1 1 640 480];

            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = app1

            % Create UIFigure and 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