How do i get the user's input in a MATLAB standalone executeable
Show older comments
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=importdata('Executeable1.bmp');
Executeable1=double(x.cdata);
x = fileread('ExecuteableInitials.txt');
fileID = fopen('ExecuteableInitials.txt','r')
fscanf(fileID,'%f')
x = input('put in initials')
and i tried the inputdlg as well.. I am at a lost
Accepted Answer
More Answers (2)
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
if coder.target('MATLAB')
% Parse the numbers using sscanf
idx(k) = sscanf(idxStr, '%d');
temp(k) = sscanf(tempStr, '%f');
else
% 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)));
end
k = k + 1;
end
fclose(f);
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
on 30 Jul 2015
0 votes
Categories
Find more on MATLAB Compiler in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!