Reading data from ASCII file
이전 댓글 표시
Hi,
I have an ASCII file with data corresponding to several runs of a simulation. I want to plot all the runs in the same plot for comparison purposes and know that I have to import the data as a matrix to begin with any other processing. However, I have been facing problems with importing the data. I have attached the file here for reference purposes.
This was my take on solving this problem:
1. Using textscan
fidi = fopen('data.txt');
D=textscan(fidi, '%u %u');
E = cell2mat(D);
However, this returned empty cells as is shown by the following command:
whos E
Name Size Bytes Class Attributes
E 0x2 0 uint32
2. Using textread
fid = 'data.txt';
B = textread(fid, '%f %f');
This returned the following errors:
Error using dataread
Number of outputs must match the number of unskipped input fields.
Error in textread (line 171)
[varargout{1:nlhs}]=dataread('file',varargin{:}); %#ok<REMFF1>
Then I changed the code to this:
[B,C]=textread(fid, '%f %f');
Which in turn returned the following errors:
Error using dataread
Trouble reading floating point number from file (row 1, field 1) ==> vds Id(M1)\n
Error in textread (line 171)
[varargout{1:nlhs}]=dataread('file',varargin{:}); %#ok<REMFF1>
3. Using spcread
B=spcread(fid);
This gave the following error:
Undefined function or variable 'spcread'.
4. Using importdata
I had limited success with this, but this was as far as I could go...
A=importdata(fid);
This gave me a 1x1 struct file with a 101x2 double comprising of the first 101 lines of the text file and a 2x1 cell with the first two header lines.
I then removed all the header files which did import the entire data, albeit without all headers and would require splitting into multiple matrices to be able to plot all of the runs in one graph (because if I recall correctly, plot function doesn't support dot indexing for variables of the form A.data) like so (output taken from a commercial spice simulator):

Could someone help me import the data properly so that I can move to plotting the curves?
채택된 답변
추가 답변 (1개)
J. Alex Lee
2020년 5월 17일
It would have been helpful to know these things in advance (you hinted at the first, but say it explicitly)
- Data contains multiple header rows for multiple sets of data
- Each data set is exactly 101 data points long
Without having known the 2nd detail, here's a script that will split your data - maybe there's more elegant ways (certainly much better ones knowing the exact length of all data sets)
fc = string(fileread('data.txt'));
flines = split(fc,newline);
% remove empty lines
flines(flines=="") = [];
HeaderMask = contains(flines,"Step");
HeaderIdx = find(HeaderMask);
% pad the indices with "ghost" line after last line
HeaderIdx(end+1) = length(flines)+1;
NSets = length(HeaderIdx) - 1;
hdrs = flines(HeaderMask)
for i = NSets:-1:1
tmp = flines(HeaderIdx(i)+1:HeaderIdx(i+1)-1);
tmp = str2double(split(tmp,char(9)));
data{i,1} = tmp;
hdrs(i,1) = flines(HeaderIdx(i));
end
댓글 수: 8
J. Alex Lee
2020년 5월 17일
If you have control of the actual simulations, it would better to make it write separate files for each run.
Pratyush Manocha
2020년 5월 18일
J. Alex Lee
2020년 5월 18일
char(9) is tab.
Stephen's answer below should be accepted.
I'm not sure if there's something to be said, or a fix for, the "append to matrix" growth of the data that it involves, though.
Stephen23
2020년 5월 18일
"I'm not sure if there's something to be said, or a fix for, the "append to matrix" growth of the data that it involves, though."
None of the numeric matrices get expanded.
The cell arrays hdr and out do get expanded on each iteration, but because these are small (eleven elements each ) they only constitute a small number of bytes (essentially pointers to the numeric arrays) and therefore expanding (i.e. copying them in memory) them is unlikely to be a significant time-cost for MATLAB's memory management.
See also:
J. Alex Lee
2020년 5월 18일
Oh I see, "out" was pre-defined as an empty cell...
Pratyush Manocha
2020년 5월 18일
J. Alex Lee
2020년 5월 18일
In this case, in my inefficient process of reading in the text data into a string, Matlab showed the tab character as a right-arrow in the command line. But as Stephen's answer indicates, I guess you don't necessarily need to know that since textscan can figure it out.
You could also copy-paste the character and do
double(' ')
Pratyush Manocha
2020년 5월 19일
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
