Textscan to convert ASCII file to mat file
11 次查看(过去 30 天)
显示 更早的评论
I would like to be able to convert large ASCII file to .mat file for further data analysis and creating plots. My script below didn't generate error but also didn't give me the resutls that I want. C turned out to be an empty 1x15 cell array which basically means that it didn't do the job. I want to be able to convert the data file into .mat files that I can just plot by calling plot(C.TestTime,C.Amps).
My codes:
fileName='JL1_JWCEVCL12C1C204356W5P0K3.006';
fileID = fopen(fileName);
% formatSpec=['%d %d %d %{dd hh:mm:ss}D %{dd hh:mm:ss}D %f32 %f32 %f32 %f32 %s %d8 %u %f %f %s %f')];
formatSpec = ['%*d %d %d %s %s %d %f %f %f %f %s %d %s %f %f %f *s' repmat('%*d', [1,47]) '%*[^\t]'];
C = textscan(fileID,formatSpec,'Delimiter','\t');
fclose(fileID);
disp C
Results :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/220014/image.png)
Part of the raw data file attached for test. The full file is large so I trimmed off a lot of data to make it <2MB.
3 个评论
Stephen23
2019-5-17
@Yu Jia: that tab-delimited file should be quite easy to import... except that it was saved with each line completely enclosed in double quotes:
"A B C ... X Y Z"
This is very unfortunate, as MATLAB's importing tools can trivially import CSV/TSV/SKV files... as long as they follow basic norms of such files (such as not making each line one text field in double quotes). Do you have any possibilty to save the file/s without the double quotes? (if this task is a one-off then the answer is likely "yes". If you have to process ten thousand such files, then this might not be an option for you...)
回答(1 个)
Fangjun Jiang
2019-5-17
编辑:Fangjun Jiang
2019-5-17
You need to
- skip two headerlines
- not to specify 'Delimiter' as '\t'
- carefully specify the format to match the file.
I got this result with the below changes.
formatSpec = ['"%d %d %d %s %s %s %s %f %f %f %f %s %d %s %f %f %f %s' repmat(' %f', [1,15]) repmat(' %d', [1,32]) '"'];
C = textscan(fileID,formatSpec,'Headerlines',2);
C =
1×65 cell array
Columns 1 through 3
{4997×1 int32} {4997×1 int32} {4997×1 int32}
Columns 4 through 6
{4997×1 cell} {4997×1 cell} {4997×1 cell}
2 个评论
per isakson
2019-5-20
Thanks so much for your response! I used your codes but still got the same results. C still shows as the empty array of 1×65 cell.
per isakson
2019-5-20
编辑:per isakson
2019-5-20
This works well on R2018b
%%
fid = fopen( 'JL5_JPC_P1_CL_12C2C0056W5CP.006.csv' );
formatSpec = [ '"%d%d%d%s%s%s%s%f%f%f%f%s%d%s%f%f%f%s' ...
, repmat('%f',[1,15]), repmat('%d',[1,32]),'"' ];
C = textscan( fid, formatSpec, 'Headerlines',2 );
fclose( fid );
%%
whos C
Name Size Bytes Class Attributes
C 1x65 5923728 cell
"C still shows as the empty array of 1×65 cell." Here C contains nearly 6 MB of data.
>> num = C{8};
>> whos num
Name Size Bytes Class Attributes
num 4997x1 39976 double
Proposal: Add 'ReturnOnError',false to the call of textscan(). That might give you a useful error message.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!