Load data into GUI editable fields from txt

I have an application with several inputs.
Currently, I input the data manually in the GUI. I want to add a push button and load a txt file that contains numbers, text, etc that goes into these fields.
Examples of the fields and the data:
app.REditField.Value = PEI
app.DEditField.Value = 1.33
app.Slider.Value = 20
% pick one
app.EButton = 0
app.SButton = 0
app.HButton = 1
I saw some examples with "eval" and "readtable" but they are not variables... Here’s what I have so far (I know it’s not much):
% Button pushed function: LoadDataButton
function LoadDataButtonPushed(app, event)
[filename] = uigetfile ({'*txt'});
end

9 Comments

Those examples: would that MATLAB code be an example of what is in one of the text files?
Or would there be a different text file per field, and you want to read the text file and store everything in it to the appropriate field?
Thanks. There is only one txt file with all the inputs for that session. The example shows a few of the inputs and how they are organized in the txt file. Basically, the inputs are either "edit field" or slider value or radio buttons.
Yes, I want to read the txt file and put all the inputs in their corresponding field so that I don't have to enter them manually.
If it looks like what is posted, complete with the app.REditField.Value = PEI then you can run() the file if you are not deployed -- and provided that PEI is a variable that is defined in context.
If PEI is intended to be a string constant then either enclose it in quotes in the file, or else process the file before executing it, to turn anything that is not a number into a string.
run()'ing the code is not the best of practices
Is it certainly the all assignments will be to within app ?
Well, the inputs are not constant so I cannot just put app.REditField.Value = PEI. The idea is to have different txt inputs for different sessions. In this txt, the value is "PEI", but in the next txt, it could be something else. That's why it's important for the program to pick this up from the txt file and put it in the right field.
For this particular session, this is how the first tab looks like when the data is manually typed in:
img1
The corresponding txt file is:
img2
Now imagine, you click on Load Data, select the txt file, and the data appears in the fields without you typing them... That's the idea.
Like this?
function LoadDataButtonPushed(app, event)
[filename] = uigetfile ({'*txt'});
Run (filename);
end
There is extra work required because run() cannot handle the ".txt" file extension. Most of the work could be avoided if you switched to naming the files as .m files.
function LoadDataButtonPushed(app, event)
[filename, folder] = uigetfile ({'*txt'});
if ~ischar(filename); return; end %user cancel
filename = fullfile(folder, filename);
tf = tempname + ".m";
copyfile(filename, tf);
cleanMe = onCleanup(@() delete(tf));
run(tf);
clear cleanME %delete file
end
It makes sense. I changed the extension to .m and it works perfectly now. Thank you so much.
By the way, is there a way to encrypt this .m input file so that you can only open it with the app?
Update: I converted to .p file (obfusicated) and it still works in the same way as .m. (I know p-code is not the same thing as encryption).
If the extension is already .m or .p then
function LoadDataButtonPushed(app, event)
[filename, folder] = uigetfile({'*.m', '*.p'});
if ~ischar(filename); return; end %user cancel
filename = fullfile(folder, filename);
run(filename);
end

Sign in to comment.

Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Tags

Asked:

on 10 Sep 2021

Commented:

on 10 Sep 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!