Matlab coder giving issue with 'textscan'

I am trying Matlab Coder for the first time. I understood that it gives conversion of my Matlab code to C code.
My data input is from a .txt file and i used 'textscan' finction in my code which is giving issue in Matlab coder.
Can anybody suggest how can i fix this.

3 comentarios

madhan ravi
madhan ravi el 28 de Dic. de 2018
upload your code
Walter Roberson
Walter Roberson el 28 de Dic. de 2018
What is the message from MATLAB Coder?
Gopika Akhil
Gopika Akhil el 28 de Dic. de 2018
portion of the code giving error is
fid=fopen('101.txt');
s=textscan(fid,'%f'); -----> issue is "textscan is not supported for code generation"
fclose(fid);
x = s{1};
xsig =x(1:1:1000);
The text file used is also attached here . It is ECG signal.

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 28 de Dic. de 2018
Editada: Stephen23 el 1 de En. de 2019

3 votos

Basically you will have to read the data as characters, and then convert to numeric. Probably the easiest way to do this is to use fread and then the method I outlined here:
Your case is simpler because you only have one single vector of data, which simplifies things quite a bit (I have not tested this with Coder, but I am sure that you can tweak it as required):
fid = fopen('101.txt');
str = fread(fid,[1,Inf],'char');
fclose(fid);
str = char(str);
idx = isstrprop(str,'wspace');
idy = diff([true,idx,true]);
idb = find(idy<0);
ide = find(idy>0)-1;
num = numel(idb);
tmp = cell(num,1);
for k = 1:num
tmp{k} = str(idb(k):ide(k));
end
vec = str2double(tmp);
Giving:
>> numel(vec)
ans = 1200
>> vec
vec =
-0.34500
-0.34500
-0.34500
-0.34500
-0.34500
-0.34500
-0.34500
-0.34500
... lots of lines here
-0.40500
-0.41500
-0.42500
-0.42000
-0.42000
-0.42000
-0.43000
-0.43000
-0.43000
EDIT (no cell array):
fid = fopen('101.txt');
str = fread(fid);
fclose(fid);
str = char(str(:).');
idx = isstrprop(str,'wspace');
idy = diff([true,idx,true]);
idb = find(idy<0);
ide = find(idy>0)-1;
num = numel(idb);
out = nan(num,1);
for k = 1:num
out(k) = str2double(str(idb(k):ide(k)));
end

2 comentarios

Walter Roberson
Walter Roberson el 31 de Dic. de 2018
coder prohibits t permission in fopen()
Stephen23
Stephen23 el 31 de Dic. de 2018
"coder prohibits t permission in fopen()"
It is not required anyway, so I got rid of that.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 28 de Dic. de 2018

0 votos

MATLAB Coder does not support textscan(). It also does not support fgetl() or fgets() or fgetc() or fscanf() or sscanf() . It does support fread(), including reading one character at a time, so you could in theory write an equivalent to fscanf() .
... But in practice you would not do that. Instead, in practice what you would do is use coder.ceval() to call C standard I/O routines to analyze and convert lines.

11 comentarios

Gopika Akhil
Gopika Akhil el 31 de Dic. de 2018
I tried to use ' fread ' . But i am not able to identify which data format will get me the correct data. Hrere the data values include positive and negative whole numbers,upto four bit of decimal values.
You would
fread(fid, 1, 'char')
to read one character at a time, and do the testing for CR and NL yourself in order to build up lines that you could interpret with str2double() .
fread() is to read binary, and will not do conversion from text to numeric for you. Normally you would fgetl() or fgets() to read a line to work with, but those are not supported by MATLAB Coder, so you would need to fread() single characters to find the boundaries.
Stephen23
Stephen23 el 31 de Dic. de 2018
Gopika Akhil's "Answer" moved here:
Yes i tried that way, but str2double is not returning the proper value.
for example, the first value in my text file is -0.345000000000000 but
fread(fid, 1, 'char') will give the value as 45.
Doing str2double for this gives NaN.
Stephen23
Stephen23 el 31 de Dic. de 2018
@Gopika Akhil: fread reads characters, and the first character in your file is a minus sign (which has character value 45). But I doubt that you want the character values of each individual character.
To convert all those characters in the file into numeric you can identify the boundaries of any number substrings, split them into those substrings, and then apply str2double to those substrings (exactly as I showed in the link I gave in my answer), or use coder.ceval as Walter Roberson suggested.
Gopika Akhil
Gopika Akhil el 31 de Dic. de 2018
I tried the steps suggested in the link you have given. Even after seperation by isstrprop and creating separate arrays and then trying str2double is giving me NaN as answer.
I am unable to find the usage of coder.ceval for textscan.
Walter Roberson
Walter Roberson el 31 de Dic. de 2018
you cannot coder.ceval of textscan unless you write your own textscan . textscan is not a C or C++ library function . you would invoke fscanf
Stephen23
Stephen23 el 31 de Dic. de 2018
"I tried the steps suggested in the link you have given. Even after seperation by isstrprop and creating separate arrays and then trying str2double is giving me NaN as answer."
It worked for me. See my answer.
Gopika Akhil
Gopika Akhil el 1 de En. de 2019
Found issues with the code now.
Code will not take inf. i replaced it with total length ie,1200 but then it results in inappropriate str.
Coder was not taking cell. But was able to correct it using repmat.
But after all this cell array inputs to 'str2double' is not supported.
Stephen23
Stephen23 el 1 de En. de 2019
Editada: Stephen23 el 1 de En. de 2019
@Gopika Akhil: if cell arrays do not work with str2double when using Coder then you can convert each substring to numeric separately:
If you have the start and end indices of each number substring then these are trivial to loop over. See my edited answer.
Gopika Akhil
Gopika Akhil el 1 de En. de 2019
Can you suggest
1)what i have to use insted of 'inf' in your code, because coder will not take 'inf'
2)how will i convert each substring to numeric separately.because i am again getting inf when i tried it before making the cell.
Stephen23
Stephen23 el 1 de En. de 2019
"what i have to use insted of 'inf' in your code, because coder will not take 'inf'"
See the "EDIT" code in my answer, which does not use Inf.
"how will i convert each substring to numeric separately.because i am again getting inf when i tried it before making the cell."
See the "EDIT" code in my answer, which does not use a cell array.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Productos

Versión

R2016a

Etiquetas

Preguntada:

el 28 de Dic. de 2018

Comentada:

el 1 de En. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by