MATLAB Answers

Passing input variables from .txt or .xlsx or .csv into MATLAB Standalone Application

2 views (last 30 days)
Kevin Syc
Kevin Syc on 26 May 2021
Commented: Allen on 30 May 2021
Hi everyone,
I have a code that deals with mixed integer linear programming and creates an array based on measurements from site survey and some constraints and objective function that I supplied to it. At the begining, I supply the code with an array like this:
EV = [2 0 -2 2 1 -4 5 2 2 2 8 0 4 10 10 10 4 6 8 2 14 10 8 4 6 8 -2 -4 12 16 4 -4 3 0]';
Now this changes from site to site, it is a track geometry (measured versines) and this is example of one such survey. My code creates a second array with design versines that tells me what the track should look like to be optimized.
Now if I was to work in MATLAB only, it would be easy to simply change the EV array whenever we have a new survey and let the code calculate the design versine for different sites. However, I compiled my MATLAB code into Standalone Application so people without MATLAB can use it on their computers. Now is there a way I can create a .txt or .csv. or .xlsx file where they can write EV (data from their surveys) and the application will take the input from this file?
So far, when I compile my code, the input is always what it was at the point of compiling the software and i can't seem to find a way to change this.
Any help would be appreciated!

Accepted Answer

Allen
Allen on 26 May 2021
Kevin,
One approach would be to create a pushbutton object with a callback to browse and import data. The following callback example was setup in App Designer, but can also be accomplished in GUIDE with a few adjustments. It also was set to import data from a *.mat data file, but can easily be modified to work with a *.txt file.
% Button pushed function: pb_LoadEVFile
function pb_LoadEVFileButtonPushed(app, event)
% I typically assign a default directory location that updates to the
% last accessed directory when saving/loading files. However you can
% make this whatever you prefer.
startdir = app.Startdir; % string value for stored starting directory
% Essentially what I use to define a default directory when my app
% launches
startdir = strcat("C:\Users\",getenv("USERNAME"),"\Documents\");
if ~exist(startdir,"dir")
startdir = "C:\";
end
% Prompt user to indicate filename and location
[fn,pn] = uigetfile(fullfile(startdir,"*.mat"),"Select Data File");
% Load data file and update App objects
load(fullfile(pn,fn),"-mat","Data");
% Run a quick check to verify the new EV data is in the correct format
% before assigning it to your app data.
nEV = []; % assign a 1x2 array with row, column dimensions required of EV data
if isfield(Data,"Standard") && isequal(Data.Standard,nEV)
app.EVData.Value = Data.Standard;
end
end

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!