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 Center および File ExchangeCreating, 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