Problem with import data

10 ビュー (過去 30 日間)
Jason
Jason 2015 年 4 月 9 日
編集済み: Stephen23 2015 年 4 月 9 日
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 件のコメント
Adam
Adam 2015 年 4 月 9 日
編集済み: Adam 2015 年 4 月 9 日
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
Jason 2015 年 4 月 9 日
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

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2015 年 4 月 9 日
編集済み: Stephen23 2015 年 4 月 9 日
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 件のコメント
Jason
Jason 2015 年 4 月 9 日
編集済み: Jason 2015 年 4 月 9 日
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.

サインインしてコメントする。

その他の回答 (1 件)

Jason
Jason 2015 年 4 月 9 日
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 件のコメント
Stephen23
Stephen23 2015 年 4 月 9 日
編集済み: Stephen23 2015 年 4 月 9 日
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 :)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT Files についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by