Load data into GUI editable fields from txt
Show older comments
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
Walter Roberson
on 10 Sep 2021
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?
Pelajar UM
on 10 Sep 2021
Walter Roberson
on 10 Sep 2021
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 ?
Pelajar UM
on 10 Sep 2021
Edited: Pelajar UM
on 10 Sep 2021
Walter Roberson
on 10 Sep 2021
run() the file.
Pelajar UM
on 10 Sep 2021
Walter Roberson
on 10 Sep 2021
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
Pelajar UM
on 10 Sep 2021
Edited: Pelajar UM
on 10 Sep 2021
Walter Roberson
on 10 Sep 2021
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
Answers (0)
Categories
Find more on App Building in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!