Info

此问题已关闭。 请重新打开它进行编辑或回答。

trouble for importing a data file

2 次查看(过去 30 天)
NDKA
NDKA 2012-3-15
关闭: MATLAB Answer Bot 2021-8-20
Hi,
I want to import a *.asc file. The file has data something like below.
0.021780 2 768 Rx d 8 21 62 254 157 2 39 255 242
0.042450 2 768 Rx d 8 21 16 2 62 0 158 255 246
I'm tried different commands, but I'm not able to load this as per my requirements.
I tried to use 'importdata' function as shown below
"[A] = importdata('CANOE10.asc', '\t');"
When I use this, all the data is stored in single cell as it is in the file. But, I want to store the data separately in different variables like
A1 = 21, B1= 62, C1 = 254, D1 = 157 etc..
I even tried 'textread' function as like below.
"[C, D, E, F, G, H] = textread('CANOE10.asc', '%s %s %s %s %s %d'); " Using this function, the data was able to store in different variables, but its not able to store consistently.
Kindly help me please.

回答(2 个)

Walter Roberson
Walter Roberson 2012-3-15

Geoff
Geoff 2012-3-15
Using textread is fine here.
But you need to include as many fields in your format string as you expect to see in the file, starting from the first. Even if you don't care about the first few columns.
Looking at that example data, I would use the format:
'%f%d%d%s%s%d%d%d%d%d%d%d%d%d'
You have to specify that many variables on the left though.
If you go back to your importdata version, it might be easier.
in = importdata('CANOE10.asc', '\t');
A = in.data(:,1);
B = in.data(:,2);
C = in.data(:,3);
% etc...
  2 个评论
NDKA
NDKA 2012-3-15
Geoff,
Thank You. When I use importdata, variable 'in' is storing the data as cell, instead of structure. i.e. the whole row is copying in to 'in' at once. So, I'm not able to see the 'data' member.
Geoff
Geoff 2012-3-15
Is your data _actually_ tab-delimited? If not, then specifying that it is will cause each row to be read into a single string.
I checked my code by copying your lines verbatim into a file and calling importdata('CANOE10.asc'); That will use spaces as the delimiter, and behaves correctly.

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by