Problem with import data
Show older comments
I have a problem using import data with the attached txt file. Unfortunately I cannot change the format of this.
(I also enclose a few lines here)
Field, Real Y, Parax Y, %Dist, R-P(um)
0.0000 0.0000 0.0000 0.0000 0.0000
0.0625 -2.0453 -2.0451 0.0094 -0.1912
0.1250 -4.0917 -4.0902 0.0374 -1.5314
My code is:
delimiterIn = ' ';
headerlinesIn = 1;
A = importdata(file,delimiterIn,headerlinesIn)
which returns: A =
'ÿþF i e l d , R e a l Y , P a r a x Y , % D i s t , R - P ( u m ) '
7 Comments
Is your question that you don't know how to get the rest of the file read in or that you don't like the odd symbols at the start?
For the former, I tend to just use textscan as e.g.
C = textscan( fid, '%s' );
and then manipulate the tokens as I see fit. You can get fancier with the filespec and delimiters if you want, especially if you read in the header row first and then you can use float specifiers as e.g.
textscan( fid, '%f %f %f %f %f' )
to read in each line (I haven't tested that exact syntax on this file, it is just as an example). You can also put together that format string dynamically rather than hard-coding 5 %fs
With regard to the odd symbols on the front I'm not sure. I see them when I open the file in Matlab editor though not in notepad. You can easily trim those off though as they are only at the start.
Jason
on 9 Apr 2015
Adam
on 9 Apr 2015
When I did a basic textscan on the whole file I got 63 tokens, 7 for the headings (2 of the headings get split in two) followed by 56 for the numbers.
If your only problem is one trailing empty cell though and it is consistent then you can just remove the last cell before you reshape.
That is strange. I edited the file to match what you said (though I may have something slightly different) and ran your expressions and I get the correct 56 tokens in C. You seem to be missing one which I assume is the first of the 0.0000 values.
I get what you had if I remove the newline character after the final title, then obviously the first line of values tags onto the title line in the file though.
In that case the first 0.000 gets attached to the final title and I get 55 values scanned afterwards, but I assume your file is not like that? Make sure you do have a proper newline after your titles though and that your last title doesn't have the first value attached.
Jason
on 9 Apr 2015
Accepted Answer
More Answers (1)
Jason
on 9 Apr 2015
0 votes
1 Comment
Unfortunately MATLAB's Unicode support is a bit light... and "text" means quite different things to different programs. The text-reading functions in MATLAB seem to assume one byte per character, which ruins file data that is encoded with multiple bytes per character (e.g. UTF-8), thus those strange blank characters you were getting.
I am glad that it is working now :)
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!