Memo/Matlab

Matlab function : Update the Text Area with part of a text file

_Ju 2022. 8. 2. 00:27

When you want to update the 'Text Area' with part of a text file,

You can use this function.

 

This example compares the task variable to be executed with the Drop Down menu, and recalls the text content by remembering the text index of the matching part.


#Text.ini

[A_Menu_1]
this is what i want to show the contents.
you can write something between [menu name] and next [menu name]
3asdasd
sadasdasdasgag

[A_Menu_2]
4
5
hello

[A_Menu_3]
you should write at least 1 line.
<End>

 

#Function

%// how to call function
TaskOrder = 'A_Menu_1';
DropDownList = {'', 'A_Menu_1', 'A_Menu_2', 'A_Menu_3'};
DesktopPath = winqueryreg('HKEY_CURRENT_USER', 'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders', 'Desktop');
txtFilePath = strcat(DesktopPath, '\text.ini');
TextBoxUpdater(TaskOrder, DropDownList, txtFilePath, app.TextBox)



%// Below is the function
function TextBoxUpdater (TaskOrder, DropDownList, txtFilePath, TextBox)

    text = regexp(fileread(txtFilePath),'\n', 'split');
    Ctn = length(DropDownList);
    for ii = 2 : (Ctn-1)
        SearchSLine = strcat('[', DropDownList(ii), ']');
        SearchELine = strcat('[', DropDownList(ii+1), ']');
        if (strcmp(TaskOrder, DropDownList(ii)))
            error = 0;
            Sline = find(contains(text, SearchSLine));
            Eline = find(contains(text, SearchELine));
            break
        elseif (ii == (Ctn-1))
            SearchSLine = strcat('[', DropDownList(Ctn), ']');
            SearchELine = strcat('<END>');
            if (strcmp(WantedWrok, DropDownList(Ctn)))
                error = 0;
                Sline = find(contains(text, SearchSLine));
                Eline = find(contains(text, SearchELine));
                break
            end
        else
            error = 1;
        end
    end

    if (~isnumeric(Sline) || ~isnumeric(Eline))
        error = 1;
    end

    if (error == 0)
        Eline = Eline - 2;
        DataArray = [];
        for ii = Sline : Eline
            text = fopen(txtPath);
            temp = textscan(text, '%s', 1, 'delimiter', '\n', 'HeaderLines', ii);
            temp = [string(temp)];
            DataArray = [DataArray; temp];
        end
        TextBox.Value = DataArray;
    elseif (error == 1)
        DataArray = {''; ''; ''; ''; 'Error : Can not found txt file or data string'};
        TextBox.Value = DataArray;
    end
end

 

#Matlab

#Matlab App Designer