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

7 vues (au cours des 30 derniers jours)
Annan Shang
Annan Shang le 29 Juil 2015
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

Réponse acceptée

Walter Roberson
Walter Roberson le 29 Juil 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 http://www.mathworks.com/products/compiler/supported/compiler_support.html
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.
  2 commentaires
Ryan Livingston
Ryan Livingston le 29 Juil 2015
When you say "you can safely coder.extrinsic() them" do you mean coder.ceval() to call the C runtime versions? Extrinsic functions are not available in standalone code.
Walter Roberson
Walter Roberson le 29 Juil 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...

Connectez-vous pour commenter.

Plus de réponses (2)

Ryan Livingston
Ryan Livingston le 29 Juil 2015
Modifié(e) : Ryan Livingston le 30 Juil 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
Annan Shang le 30 Juil 2015
Thanks a lot for the help, both solutions helped me.

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by