create a todo list app in matlab

To create a todo list app in MATLAB, we will utilize App Designer, a visual environment for designing apps in MATLAB. Here are the steps to create a simple todo list app in MATLAB:

  1. Open MATLAB and launch App Designer.
  2. From the "Design" tab, drag and drop a "Listbox" component onto the app canvas.
  3. In the "Properties" tab, rename the listbox to "todoList" and change its "Multiselect" property to "on".
  4. Create an "Edit Field" component and rename it to "todoItem".
  5. Add a "Push Button" component and rename it to "Add Item".
  6. Double-click the "Add Item" button to create a callback function for it.
  7. In the callback function, get the value of the "todoItem" edit field and add it to the "todoList" listbox using the "listbox.String" property. (Don't forget to clear the "todoItem" edit field afterward).
  8. Add another "Push Button" component and rename it to "Remove Item".
  9. Double-click the "Remove Item" button to create a callback function for it.
  10. In the callback function, get the value of the "todoList" listbox's "Value" property and remove those items from the listbox by setting the "listbox.String" property to a new list that does not include the removed items.

Here is the complete code for the todo list app in MATLAB:

main.m
classdef todoListApp < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure       matlab.ui.Figure
        todoList       matlab.ui.control.Listbox
        todoItem       matlab.ui.control.EditField
        AddItemButton  matlab.ui.control.Button
        RemoveItemButton  matlab.ui.control.Button
    end

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

        % Code that executes after component creation
        function startupFcn(app)
            % Initialize the listbox
            app.todoList.String = {};
        end

        % Button pushed function: AddItemButton
        function AddItemButtonPushed(app, event)
            % Get the value of the todoItem edit field
            item = app.todoItem.Value;

            % Add the item to the todoList listbox
            app.todoList.String = [app.todoList.String; item];

            % Clear the todoItem edit field
            app.todoItem.Value = '';
        end

        % Button pushed function: RemoveItemButton
        function RemoveItemButtonPushed(app, event)
            % Get the selected items from the todoList listbox
            selectedItems = app.todoList.Value;

            % Remove the selected items from the todoList listbox
            app.todoList.String(ismember(app.todoList.String, selectedItems)) = [];
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = todoListApp

            % Create UIFigure and components
            app.UIFigure = uifigure;
            app.UIFigure.Position = [100 100 330 312];
            app.UIFigure.Name = 'todoList';

            app.todoList = uilistbox(app.UIFigure);
            app.todoList.Multiselect = 'on';
            app.todoList.Position = [23 72 283 170];

            app.todoItem = uieditfield(app.UIFigure, 'text');
            app.todoItem.Position = [23 23 195 22];

            app.AddItemButton = uibutton(app.UIFigure, 'push');
            app.AddItemButton.ButtonPushedFcn = createCallbackFcn(app, @AddItemButtonPushed, true);
            app.AddItemButton.Position = [223 23 83 22];
            app.AddItemButton.Text = 'Add Item';

            app.RemoveItemButton = uibutton(app.UIFigure, 'push');
            app.RemoveItemButton.ButtonPushedFcn = createCallbackFcn(app, @RemoveItemButtonPushed, true);
            app.RemoveItemButton.Position = [23 256 283 22];
            app.RemoveItemButton.Text = 'Remove Selected Item(s)';

            % Call the app startup function
            startupFcn(app);

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

To run this app, simply create a new instance of the todoListApp class and the app will open in a new window.

gistlibby LogSnag