필터 지우기
필터 지우기

Matlab coder giving issue with 'textscan'

조회 수: 8 (최근 30일)
Gopika Akhil
Gopika Akhil 2018년 12월 28일
댓글: Stephen23 2019년 1월 1일
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
Walter Roberson
Walter Roberson 2018년 12월 28일
What is the message from MATLAB Coder?
Gopika Akhil
Gopika Akhil 2018년 12월 28일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2018년 12월 28일
편집: Stephen23 2019년 1월 1일
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
Walter Roberson
Walter Roberson 2018년 12월 31일
coder prohibits t permission in fopen()
Stephen23
Stephen23 2018년 12월 31일
"coder prohibits t permission in fopen()"
It is not required anyway, so I got rid of that.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 12월 28일
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
Gopika Akhil
Gopika Akhil 2019년 1월 1일
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 2019년 1월 1일
"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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by