How to import data from files containing a mix of numbers and strings into a Matlab cell array?
Ältere Kommentare anzeigen
Hello everybody, I have about thirty text files of varying dimensions in which the average is 2050x1450. As the figure shows, the first two rows are of type char indicating the number of profile and the recording time respectively. the remaining rows are values of type double.

with this structure, I found difficulties to assigh the content of those files into a cell array under matlab with the command 'importdata'. When i execute this:
PathName = uigetdir;
d = dir(fullfile(PathName,'*.txt'));
for i = 1:numel(d)
A(i) = importdata(fullfile(PathName, d(i).name),'\t');
end
>> A(1).textdata{1,1}
ans =
#1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 ......
>> A(1).textdata{1,2}
ans =
[]
>> A(1).textdata{2,1}
ans =
Time
>> A(1).textdata{2,2}
ans =
23:56:08
My first line is all stored in a single box A(1).textdata{1,1} . However, A(1).textdata{1,2} up to A(1).textdata{1,1449} are completely empty,contrary to what I get with the second row.

Antworten (2)
per isakson
am 31 Jul. 2014
0 Stimmen
Matlab doesn't honor comma as decimal separator. See http://www.mathworks.com/matlabcentral/answers/57276#answer_69283
1 Kommentar
amine
am 1 Aug. 2014
per isakson
am 2 Aug. 2014
Bearbeitet: per isakson
am 2 Aug. 2014
I converted your image to text, replaced the "," and saved the result in a text-file. The resulting file is straightforward to read. See below.
I assume/guess that the reasons to your problem are
- importdata doesn't correctly recognize the encoding of you file. What is the encoding? I used "ANSI" for my text file, 150615_Capture.txt. See fopen, encodingIn
- "My first line is all stored in a single box" isn't that in accordance with the documentation?
- "content of those files into a cell array"   doc says: "importdata combines the outputs into a struct array."
 
IMPORTDATA
>> A = importdata( filespec,' ');
>> A
A =
data: [26x11 double]
textdata: {2x11 cell}
colheaders: {1x11 cell}
>> A.textdata
ans =
Columns 1 through 6
[1x70 char] [] [] [] [] []
'Time' '23:56:08' '23:57:05' '23:58:02' '23:58:59' '23:59:56'
Columns 7 through 11
[] [] [] [] []
'00:00:53' '00:01:50' '00:02:47' '00:03:45' '00:04:42'
where 'h:\m\cssm\150615_Capture.txt'
Wi #2 #3 #4 #5 #6 #7 #8 #9 #10
Time 23:56:08 23:57:05 23:58:02 23:58:59 23:59:56 00:00:53 ...
15 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
30 3.17 3.58 3.50 4.03 3.90 3.25 3.60 3.47 3.13 3.75
45 4.65 4.75 5.15 5.55 4.92 5.27 5.08 5.32 5.28 4.80
...
Kategorien
Mehr zu Text Files finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!