Load function not properly loading a numeric ASCII file

I am trying to load and ASCII file that is 24X48. The load function will not load the load the actual values in the work space. It will load zeros. The "readtable" will work without a problem but it puts in format has to be converted into an array and can only be done after individual values are pulled out, put into a 1X1 table then converted into an array. I am using R2018a at this time. I never had this problem with my old version R2008b.
Is there a way to use the load function on a ASCII file that is in the form of a table that will not cause this issue?
Ideally, I need a function that can load in an ASCII file into the workspace as an array quickly without jumping through hoops and allow the data to be either a character or an integer or number?

9 个评论

You may want to take a look at this function that can handle mixed types in a delimited file: https://www.mathworks.com/matlabcentral/fileexchange/10946-readtext
Please attach an example of the file.
If you have a table, T, that consists entirely of numeric values, then
T{:,:}
would be a numeric array of the values.
Here is an example of the data and how it is formatted.
Looks to me as if it might be tab delimited. I do not see any reason for load() to load it as zeros, though. It would be better if you could attach an extract rather than just an image of a portion of it.
Here is the full file attached. This file is tab delimited. I have used this type of file many times over with R2008b but some reason, the new version (R2018a) will not load this correctly.
Try giving the command
format long g
and then displaying the data again.
After some more research, the .txt files in question originate from a Unix/Lnix work station. It may have somthing to do with how the file "new line" command works between Unix and PC.
I have also looked at and tested the 'readtext' function mentioned in the first comment. This will read the files but the function puts it at a cell array not a standard array or .mat file. This is workable but I have to pull each cell out separately and then do a number of steps to get them into a standard .mat format so I can run my calculations on them. This is not ideal as it requires a number of different steps to get it to work. Ideally, I would like a function that will just read any text in as a standard array (.mat) file.
The load function imported it correctly when I tried it. (See my Answer.)
I just tried it on R2018a on Windows 10. load() worked fine for me. I did have to use
format long g
to be able to see the full range of values.

请先登录,再进行评论。

 采纳的回答

Your file is (24 x 47), and load reads it correctly and completely:
D = load('20180919N.txt');
I copied your entire file to my workspace, as well as imported it with load, then did a logical test for equality between the two matrices. The two matrices are the same.

8 个评论

Tried many times, it still loads the file as zeros. Here is the file in question and a screen capture of what I get when I load it. I am only having this problem with txt files originating from a Unix/Lnix based system to a PC based system. Any txt file that originates on a PC, I don't have this problem. This makes no sense why this would be happening.
They are not all zeros.
In the upper left corner:
x =
1E+09 *
and ‘x(:,3:4)’ are therefore 2.0181E+9. The other values are less than 1E+5, so they simply display as zeros.
It appears to be loading them correctly. If you wan t to see all the details, before you read the file, specify:
format long g
When I do this:
x = load('20180929n.txt');
format long g
x
I get this:
x =
Columns 1 through 4
37.66 -97.44 2018092907 2018100207
39.0294 -98.13 2018092907 2018100207
38.3614 -98.828 2018092907 2018100207
38.2936 -97.9113 2018092907 2018100207
37.6454 -98.124 2018092907 2018100207
38.078 -96.8877 2018092907 2018100207
...
So load is importing your data. Your current format setting is simply not displaying it all. There is nothing wrong anywhere, as I see it.
Hmmm....Knew it had to be something simple. Is there a setting in MatLab to display at "long" by default. I have looked at I can't seem to get it to do that. WHile this may be simple, this does cause a problem when working with the data. So I need to some how prevent Matlab from assuming I want it in this format. I prefer to see it not in Sci Notation unless I am dealing with numbers with more than 12 digits, which is not common for me.
Ok. Thanks for the help. Leave it to me to have an issue that is an easy fix. LOL
You can easily switch between format options. I usually have mine set to:
format short eng
so when I read your file and displayed it, I wrote:
x = load('20180929n.txt');
format long g
x
format short eng
That displayed it as I wanted, so I could post the result, and then reset it to my desired option. See the documentation on format (link) for all the available options.
To set the default format:
... format long g is what I had suggested a week ago.
Wanted to thank all that contributed to this question. While I have used MatLab in the past, I am not just getting back into it and some of the simple issues like format I am having to relearn. Would have posted this sooner but I have been dealing with a number of issues. This is the first chance I have had to get back to this. This did work and it is now a standard in my scripts.
Our pleasure.
MATLAB has changed over the years, and even between R2018a and 2018b. See the Release Notes (link) for a release-by-release synopsis. It now also has a search function.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by