How do I use fprintf with MATLAB Coder?

50 views (last 30 days)
I'm trying to use fprintf with MATLAB Coder but struggling to understand the limitation of "The formatSpec parameter must be constant."
I'm simply trying to save an array as an CSV or text file, and using the following code/function:
function write2CSV(varname, fname)
% Writes a particular variable to CSV file.
if nargin<2, fname = 'test.csv';end
fileID = fopen(fname,'w');
%fprintf(fileID,'%7.4f\n',varname);
fprintf(fileID,"%c\n",varname);
fclose(fileID);
You can call this function inside a script with any simple array as input such as:
write2CSV([1;2;3;4;5]);
It works fine in basic MATLAB. But when I try to generate C-code using MATLAB Coder, the specific error I'm getting is ... "argument corresponding to conversion character 'c' in the formatspec parameter is not scalar. It has to be fixed size scalar". HOW DO I SPECIFY A FIXED-SIZE SCALAR IN THE FORMAT SPEC?
I have tried various combinations for the format spec, but don't understand how to make it a 'constant'.
Any ideas? thanks a bunch!

Accepted Answer

Darshan Ramakant Bhat
Darshan Ramakant Bhat on 7 Sep 2020
How are you generating code ? Are you using Coder App or Command Line ?
As the error says, you cannot pass an array as input when the format specifier is "%c". I modified the code like below and it worked for me :
function write2CSV(varname, fname)
% Writes a particular variable to CSV file.
if nargin<2, fname = 'test.csv';end
fileID = fopen(fname,'w');
%fprintf(fileID,'%7.4f\n',varname);
n = length(varname);
for i = 1:n
fprintf(fileID,"%c\n",varname(i));
end
fclose(fileID);
Code generation using command line :
codegen write2CSV -args {'hello','myFile.txt'} -report -config:lib
Hope this will be helpful
  4 Comments
Arfoss Andy
Arfoss Andy on 8 Sep 2020
I did try %s, but that gave the same error. Looks like either way, I have to use a For loop to print it one at a time.
Also, can anyone point out how to create a EXE that'll execute this code on my host PC as a standalone program? Looks like I might have to modify the generated Main.c - and it's not straightforward to create a standlone EXE.
All I'm trying to do here is to simply read in using fscanf, and write it back using frpintf a bunch of numbers from CSV. Once this works, I plan to introduce processing within this code to modify the numbers that are read in, before I save them again.
Here's the full MATLAB Code - that now work for generating code. I'll try to explore creating an EXE next.
%% MAIN FUNCTION TO DO THE READING AND WRITING
function coderTests(file2write)
fname = 'myInputFile.csv';
%Read a specific CSV file
a = getCSV2MAT(fname); %
% Some Processing goes here eventually on a
% Write it back to another name.
write2CSV(a,file2write);
%% FUNCTION TO READ CSV
function out = getCSV2MAT(fname)
% Uses fscanf to read variables from csv file into a matrix
fileID = fopen(fname,'r');
fgets(fileID); % Skip the first line which is the header
formatSpec = '%d\n';
out = fscanf(fileID,formatSpec);
fclose(fileID);
%% FUNCTION TO WRITE TO CSV
function write2CSV(varname, fname)
% Writes a particular variable to CSV file.
if nargin<2, fname = 'test.csv';end
fileID = fopen(fname,'w');
for i = 1: length(varname)
fprintf(fileID,"%d\n",int32(varname(i)));
end
fclose(fileID);
Darshan Ramakant Bhat
Darshan Ramakant Bhat on 9 Sep 2020
I tried below code and it worked for me without error
function write2CSV(varname, fname)
% Writes a particular variable to CSV file.
if nargin<2, fname = 'test.csv';end
fileID = fopen(fname,'w');
%fprintf(fileID,'%7.4f\n',varname);
% n = length(varname);
%
% for i = 1:n
% fprintf(fileID,"%c\n",varname(i));
% end
fprintf(fileID,"%s\n",varname);
fclose(fileID);
codegen -config cfg write2CSV -args {coder.typeof('a',[inf inf]),coder.typeof('a',[inf inf])} -report
For EXE :
cfg = coder.config('exe');
cfg.GenerateExampleMain = "GenerateCodeAndCompile";
Above will generate EXE file with the default example main. We also generate makefile inside the codegen folder. You can modify the generated main and try to rebuild EXE using the makefile.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!