Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

trouble for importing a data file

2 vues (au cours des 30 derniers jours)
NDKA
NDKA le 15 Mar 2012
Clôturé : MATLAB Answer Bot le 20 Août 2021
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.

Réponses (2)

Walter Roberson
Walter Roberson le 15 Mar 2012

Geoff
Geoff le 15 Mar 2012
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 commentaires
NDKA
NDKA le 15 Mar 2012
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 le 15 Mar 2012
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.

Cette question est clôturée.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by