필터 지우기
필터 지우기

access data read from file

조회 수: 2 (최근 30일)
F ziegler
F ziegler 2016년 12월 27일
답변: F ziegler 2017년 1월 2일
Trying to read a text file of real numbers into an array, then output the data sequentially as part of a matlab function in Simulink. Main problem is, if I don't initialize the array "RF=zeros(Filesize,1)" the "Output=RF(CountRom/2)" complains "Subscripting into an mxArray is not supported". Setting "Filesize" requires prior knowledge of the length of the file. If "Filesize" is wrong, the fscanf complains that the "read" and "expected" don't match. I'm sure there's a simple solution, just not sure what it is.
function Output = ReadTxt()
coder.extrinsic('fopen')
coder.extrinsic('fclose')
coder.extrinsic('fscanf')
FileSize = 29866;
persistent CountRom RF
if isempty(CountRom)
CountRom = uint32(0);
RF = zeros(FileSize,1);
fid = -1;
fid = fopen('Filename.txt', 'r');
if (fid > 0)
RF=fscanf(fid, '%f');
fclose(fid);
else
error('File open failed');
end
end
if (CountRom < FileSize)
CountRom = CountRom + 1;
Output = RF(CountRom / 2); % note, RF is base 1
else
fclose('all');
Output = 0;
end
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2016년 12월 27일
You might be able to get further with coder.varsize()

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

답변 (2개)

Prateek Khandelwal
Prateek Khandelwal 2017년 1월 2일
Have you considered using 'From File' block ? if your aim is to get data from the file at each time step, this might be the solution for you.
Here is the doc link: https://www.mathworks.com/help/matlab/ref/fscanf.html

F ziegler
F ziegler 2017년 1월 2일
I'm also capturing responses in the same script and both are relative to a mask parameter.

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by