Problem with load function for ASCII files

조회 수: 7 (최근 30일)
Norm Nelson
Norm Nelson 2012년 6월 20일
Hi,
I'm unable to load some ASCII files in R2012a. I can open the file in the editor and confirm it's formatted properly (rectangular array of floating point numbers, comments at top of file begun with '%'), so it's not a problem with permissions. When I try to load() the files it returns empty values.
the files look like this, roughly:
% temp.txt
81 0.3 2.364483884 0.075 0.045 0 0 2
81 0.3 2.389637968 0.075 0.045 0 0 2
81 0.3 2.739698969 0.075 0.045 0 0 2
81 0.3 2.662140543 0.075 0.045 0 0 2
...
This is a transcript of me trying to load the file and confirm it's loaded.
>> load temp.txt
>> whos
Name Size Bytes Class Attributes
temp 0x0 0 double
>>
There are no error messages, as you can see, just returning an empty matrix. No idea how to proceed from here.
  댓글 수: 5
Norm Nelson
Norm Nelson 2012년 6월 20일
Also, these files opened just fine in R2008 ... !
Norm Nelson
Norm Nelson 2012년 6월 20일
Unfortunately, this will require me to revise a large number of scripts that currently use load() to load ascii files. Why doesn't load() work for these files any more, I guess is my real question. Thanks!

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

채택된 답변

Norm Nelson
Norm Nelson 2012년 6월 20일
importdata() works best: reads the files correctly without me having to edit the files. Thanks, per.
I'm baffled as to why MATLAB's behavior has changed with regards to something as fundamental as load(). This will mean a huge amount of work to correct. Oh well.
  댓글 수: 1
per isakson
per isakson 2012년 6월 23일
Is it really the comment line, which causes the failed read?

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

추가 답변 (1개)

per isakson
per isakson 2012년 6월 20일
Did you try
dlmread
Read ASCII-delimited file of numeric data into matrix
.
--- Cont. ---
I cannot make dlmread handle comment lines - regardless of how I set the range.
.
--- Revisit the OP's question ---
I find it difficult to remember all the details of all the functions, which read ascii-data. When faced with a failed read it is too easy to jump into the wrong conclusions. I need a "selection guide". Nevermind, I copy&pasted the five lines of OP into cssm.txt and cssm.mat. The two files are thus identical but for the extension. Then I run (R2012a, 64bit, Windows7)
load cssm.txt % ok
load cssm.mat % error
load cssm.mat -ascii % ok
importdata cssm.txt % ok
importdata cssm.mat % error
.
load cssm.mat caused the error message
Error using load
Unable to read MAT-file h:\m\cssm\cssm.mat: not a binary MAT-file.
Try LOAD -ASCII to read as text.
and importdata cssm.mat caused the warning
Warning: File contains uninterpretable data.
> In importdata at 144
and returned an empty result
ans =
data: []
textdata: {}
Yes, I have checked twice that the correct data is assign in the "ok-cases".
Thus, I believe there is some other problem with the file of OP.
  댓글 수: 2
Norm Nelson
Norm Nelson 2012년 6월 20일
yes, this works, if I strip the comments from the file
Walter Roberson
Walter Roberson 2012년 6월 20일
dlmread() has an option to specify range.
You could put your own load() routine earlier in the path. Or you could do a batch edit to change load to your own routine name (a safer approach.)

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

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by