How do i get the user's input in a MATLAB standalone executeable

30 views (last 30 days)
I want the user to tell me the size that he/she wishes to use and the file he/she wishes to work on. fileread,fscanf,input,inputdlg, imread, xlsread, importdata are not supported for a standalone code generation. Does anyone know what is the best practice to get the user's input in a standalone MATLAB executeable? Things i've tried:
Initials = xlsread('ExecuteableInitials.xlsx');
x = fileread('ExecuteableInitials.txt');
fileID = fopen('ExecuteableInitials.txt','r')
x = input('put in initials')
and i tried the inputdlg as well.. I am at a lost

Accepted Answer

Walter Roberson
Walter Roberson on 29 Jul 2015
I believe you are confusing two processes here.
Mathworks has a product MATLAB Compiler, which produces stand-alone executables that can run on any operating system that MATLAB itself is supported on, running as normal programs on those systems, requiring that the operating system be present. The aspects of MATLAB for that are not supported are mostly the tools that Mathworks supplies to use the facilities interactively, such as the Curve Fitting Tool, or the Neural Network Tool. See
All of the routines you list are supported by MATLAB Compiler (though some of them were not supported until a decade ago.)
Mathworks also has a product MATLAB Coder, which produces C or C++ code, that you can then compile using your favorite compiler, including compiling for embedded systems and running on systems that have Real Time Operating Systems or have no operating system at all.
MATLAB Coder is restricted to supporting I/O routines that have direct equivalents in the C standard library; in particular it supports: fclose, feof, fopen, fprintf, fread, frewind . This does have some curious limitations compared to the C standard library -- no fgetc(), fgetl(), fscanf(), or fwrite() for example.
The good thing about the C standard library routines is that you can safely coder.extrinsic() them since you know they will exist. But otherwise you can fread() a single character at a time and synthesize your own sscanf using text processing (I wouldn't bother; that's what public access code is for, saving you from having to reimplement such things.)
If you are trying to generate C code then you either need to give up on using graphics or you need to coder.extrinsic some graphics routine that is relevant to your target system. But that's a different matter than "standalone executables", which support all of these routines already.
Walter Roberson
Walter Roberson on 29 Jul 2015
Ah, I confuse the features. Some day the Great Pumpkin is going to rise up out of the Pumpkin Patch and bring me licenses for these toolboxes so I actually test instead of going by my memory of what the documentation said before I got locked out of it due to not owning the product...

Sign in to comment.

More Answers (2)

Ryan Livingston
Ryan Livingston on 29 Jul 2015
Edited: Ryan Livingston on 30 Jul 2015
This answer will be for the MATLAB Coder workflow.
Similar to what Walter says, you could also fread the entire file into a string. And then use coder.ceval to call the C sscanf from your generated code MATLAB Coder. For example, if you have CSV file with the format:
1, 221.34
2, 125.36
3, 98.27
something like the following could read it in MATLAB Coder:
function [idx, temp, outStr] = readCsv(fname)
% Example of reading in 8-bit ASCII data in a CSV file with FREAD and
% parsing it to extract the contained fields.
NULL = char(0);
f = fopen(fname, 'r');
N = 3;
fileString = fread(f, [1, Inf], '*char');
outStr = fileString;
% Allocate storage for the outputs
idx = coder.nullcopy(zeros(1,N,'int32'));
temp = coder.nullcopy(zeros(1,N));
k = 1;
while ~isempty(fileString)
% Tokenize the string on comma and newline reading an
% index value followed by a temperature value
dlm = [',', char(10)];
[idxStr,fileString] = strtok(fileString, dlm);
fprintf('Parsed index: %s\n', idxStr);
[tempStr,fileString] = strtok(fileString, dlm);
fprintf('Parsed temp: %s\n', tempStr);
% Convert the numeric strings to numbers
% Parse the numbers using sscanf
idx(k) = sscanf(idxStr, '%d');
temp(k) = sscanf(tempStr, '%f');
% Call C sscanf instead. Note the '%lf' to read a double.
coder.ceval('sscanf', [idxStr, NULL], ['%d', NULL], coder.wref(idx(k)));
coder.ceval('sscanf', [tempStr, NULL], ['%lf', NULL], coder.wref(temp(k)));
k = k + 1;
Another option, since you are generating a standalone executable, is to write the input code in C or C++ directly in your main function. Then you can pass the necessary data to the MATLAB Coder generated code.

Annan Shang
Annan Shang on 30 Jul 2015
Thanks a lot for the help, both solutions helped me.

Community Treasure Hunt

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

Start Hunting!