Problem with import data

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

Adam
Adam on 9 Apr 2015
Edited: Adam on 9 Apr 2015
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.
Its the numbers I require, in particular I need to plot the first column against the last column. Thanks
Jason
Jason on 9 Apr 2015
Edited: Jason on 9 Apr 2015
also adding, that the result of text scan is that there is an additional element, this being C{1}{61} which shouldn't be there,
C{1}{60} =
- 1 9 7 . 8 7 6
C{1}{61} =
so when I try to reshape using
B=reshape(C,[12,5])
I get the error
Error using reshape
To RESHAPE the number of elements must not change.
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.
Jason
Jason on 9 Apr 2015
Edited: Jason on 9 Apr 2015
I have now tablimited by data including the titles: (This is my data in full, and won't change)
Field RealY ParaxY %Dist R-P(um)
0.000 0.000 0.000 0.000 0.000
0.063 -2.045 -2.045 0.009 -0.191
0.125 -4.092 -4.090 0.037 -1.531
0.188 -6.140 -6.135 0.084 -5.177
0.250 -8.193 -8.180 0.150 -12.301
0.313 -10.249 -10.225 0.236 -24.099
0.375 -12.312 -12.270 0.341 -41.799
0.438 -14.382 -14.316 0.466 -66.673
0.500 -16.461 -16.361 0.611 -100.041
0.563 -18.549 -18.406 0.778 -143.288
0.625 -20.649 -20.451 0.968 -197.876
After reading the headers:
Titles= textscan(fid,'%s',5,'delimiter','\t'); % Read strings tab delimited
I read the numbers (there are 55 of them)
C=textscan(fid,'%s',55,'delimiter','\t');
The last item displayed is:
C{1}{55} =
- 1 9 7 . 8 7 6
So when I try a reshape on C (i.e. the numbers & there are definetly only 55 of them (thats 11 rows of 5 columns)
B=reshape(C,[11,5])
I still get:
Error using reshape
To RESHAPE the number of elements must not change.
Error in Distortion>pbDraw_Callback (line 322)
B=reshape(C,[11,5])
Adam
Adam on 9 Apr 2015
Edited: Adam on 9 Apr 2015
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.
Hi Adam, this is all very strange, Ive enclosed my newest txt file.
I have found that this works, but I don't understand it.
CNew = cat(1,C{:})
and then I can reshape using:
B=reshape(CNEW,[11,5])
I also can't quite get the syntax to plot column 1 against column 5

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 9 Apr 2015
Edited: Stephen23 on 9 Apr 2015
There is something strange going on with that data-file, but if the file is a simple tab-delimited text-file (attached), then this code will import and plot that data:
fid = fopen('distortion.txt','rt');
M = cell2mat(textscan(fid,'%f%f%f%f%f','HeaderLines',1));
fclose(fid);
plot(M(:,5),M(:,1))

1 Comment

Jason
Jason on 9 Apr 2015
Edited: Jason on 9 Apr 2015
Thanks, Im still not able to get this with my text file. why is there something strange when notepad opens it ok?
The program i used to generate it is called Zemax. There was an option to use ASCII rather than UNICODE, but Im still unable to read it in matlab.
Im using this, and it doesn't like the cell2mat part:
fid = fopen(file,'rt');
Titles= textscan(fid,'%s',5,'delimiter','\t') % Read strings tab delimited
C=textscan(fid,'%s',55,'delimiter','\t')
%N=textscan(fid,'%f%f%f%f%f','HeaderLines',1)
%M = cell2mat(N)
%CNew = cat(1,C{:})
M=cell2mat(C)
Error using cell2mat (line 52)
CELL2MAT does not support cell arrays containing cell arrays or objects.

Sign in to comment.

More Answers (1)

Jason
Jason on 9 Apr 2015

0 votes

Problem solved. The program I use (Zemax) has to be told explicitly to output text in ascii rather than unicode. I now works using Stephens code, thankyou Adam and Stephen. Jason

1 Comment

Stephen23
Stephen23 on 9 Apr 2015
Edited: Stephen23 on 9 Apr 2015
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 :)

Sign in to comment.

Tags

Asked:

on 9 Apr 2015

Edited:

on 9 Apr 2015

Community Treasure Hunt

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

Start Hunting!