开发者

matlab import text format

The txt file is as follows.

  1      1     0       
  2      1     3     
         2     9     
         3    10      
  3      1     1     

When I use importdata to import the file and get the following format;

1   1   0
2   1   3
2   9   NaN
3   10  NaN开发者_如何学编程
3   1   1

What I actually want is

  1      1     0       
  2      1     3     
NaN      2     9     
NaN      3    10      
  3      1     1    

Is there any convenient way? or should I make a tab between each number before importing to let matlab recognize the first element is NaN? Thanks!

Edit

I tested and found that if the delimiter is tab and its format is correct. I am wondering if any other easy approach? Thanks.


I think the problem is that importdata() doesn't distinguish between 1 or several spaces, so it assumes that the line starts with the first number. Tab-delimited data doesn't have this issue. If you are creating the data sets, then it's probably best to separate the data using something like tabs or commas. You may be able to pull off some text formatting magic using textscan() though.


The easiest way to import this would be to do it through the Import Tool, as a Fixed Width file:

uiimport(filename);

You can also generate code from here (select the Import Selection dropdown, and generate script or function).

Alternatively, you can read it in using textscan:

fid = fopen(filename);
d = textscan(fid, '%3s%3s%s%[^\n\r]', 'Delimiter', '', 'WhiteSpace', '');
fclose(fid);

Then you could form it into a numeric array, if you want:

d2 = cell2mat(cellfun(@(x) str2double(x), d, 'UniformOutput', false));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜