When you want to save and load UI values into the text file,
You can use this function.
This example saves UI values to a text file and can be loaded back as UI values.
#Setting.ini
[Last Setting]
Set_IP = 100.100.100.100
Set_User = kim
Set_Path = C:\Users\MyName\Desktop\
Set_Option = Mellon
#Function 1 (Save)
%// how to call function
ValueList = {app.EditField1.Value, app.EditField2.Value, app.EditField3.Value, app.Dropdown.Value}
DesktopPath = winqueryreg('HKEY_CURRENT_USER', 'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders', 'Desktop');
txtFilePath = strcat(DesktopPath, '\Setting.ini');
SaveSetting(txtFilePath, ValueList)
%// Below is the function
function SaveSetting(txtFilePath, ValueList)
TargetTextName = {'Set_IP', 'Set_User', 'Set_Path', 'Set_Option'}
if (exist(txtFilePath, "file"))
fileContent = fileread(txtFilePath);
for ii = 1 : 4
exp = strcat('(?<=', TargetTextName(ii), '\s*=)', '\s.*');
newVal = strcat(" ", ValueList(ii));
if (ii == 1); %// only first time
NewTextSet = regexprep(fileContent, exp, newVal, 'once', 'dotexceptnewline');
elseif (ii == 3); %// if the value is about Path
newVal = regexprep(newVal, '\', '\\\'); %// if u dont this, '\'will be disapear in some Matlab Ver.
NewTextSet = regexprep(NewTextSet, exp, newVal, 'once', 'dotexceptnewline');
else
NewTextSet = regexprep(NewTextSet, exp, newVal, 'once', 'dotexceptnewline');
end
end
fid = fopen(txtFilePath, 'w', 'n', 'UTF-8');
fwrite(fid, txtFilePath, 'char');
fclose(fid);
else
disp("Can not foud Setting ini file. Creating new ini file");
fid = fopen(txtFilePath, 'w', 'n', 'UTF-8');
printf(fid, '[Last Setting]\n');
printf(fid, ' Set_IP = %s\n', string(ValueList(1)));
printf(fid, ' Set_User = %s\n', string(ValueList(2)));
printf(fid, ' Set_Path = %s\n', string(ValueList(3)));
printf(fid, ' Set_Option = %s\n', string(ValueList(4)));
printf(fid, '\n\n<END>\n');
end
end
#Function 2 (Load)
%// how to call function
ValueList = {app.EditField1, app.EditField2, app.EditField3, app.Dropdown};
DesktopPath = winqueryreg('HKEY_CURRENT_USER', 'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders', 'Desktop');
txtFilePath = strcat(DesktopPath, '\Setting.ini');
if exist(txtFilePath, 'file')
LoadSetting(txtFilePath, TargetLists);
end
%// Below is the function
function LoadSetting(txtFilePath, TargetLists)
error = 0;
TargetTextName = {'Set_IP', 'Set_User', 'Set_Path', 'Set_Option'};
TargetStartLine = {};
text = regexp(fileread(txtFilePath), '\n', 'split');
for ii = 1 : 4
temp = find(contains(text, TargetTextName{ii}));
TargetStartLine{ii} = temp;
if(isempty(TargetStartLine{ii}))
error = 1;
end
end
if (error == 0)
for ii = 1 : 4
text = fopen(txtFilePath);
temp = textscan(text, '%s', 1, 'delimiter', '\n', 'HeaderLines', (TargetTextName{ii}-1));
%// In text values, all previous contents are removed as a head line, and the contents are separated by \n. And display up to the first line in string format.
temp = string(temp);
newStr = split(temp, "=");
data = strtrim(newStr(2));
%// Divide the line by =, And remove the whitespace before and after it.
TargetLists(ii).Value = data;
end
end
end
'Memo > Matlab' 카테고리의 다른 글
Matlab function : Change the Enable of several UIs (0) | 2022.08.09 |
---|---|
Matlab : Make & Write Data in PPT file (without PPT API) (0) | 2022.08.09 |
Matlab function : Update the Text Area with part of a text file (0) | 2022.08.02 |
Matlab : UI drop-down (0) | 2022.08.01 |
Matlab function : Path Checker (0) | 2022.08.01 |