Convert Table to Matrix
이전 댓글 표시
Hey everyone,
I know this has been discussed in the community quite often, but after going through the answers for four hours know, I still havent found a way to handle my problem, so i would be very thankful if someone of you guys could help me out! I have data which looks like this and which is separated by two spaces:

if i try to import it with the following code (I've already tried it with readmatrix, it doesn't work):
clear all
clear
filename=uigetfile('*.dat');
A=readtable(filename,'Delimiter','\t');
I know get a table with the format 370x1, which is useless to me, because I am trying to do a surf plot with all the numeric data on the right. The Table I get looks like this:

I would be really glad If somebody could help me out, because right now, I am prepreparing the data in excel, and the plot and everything else works perfectly fine, but I would like to it directly with the dat file!
Thank you in advance and best regards from germany, Lukas
댓글 수: 8
Cris LaPierre
2021년 2월 2일
It would be easier to propose a solution if you shared your data file. You can attach it using the paperclip icon. If it doesn't like the extension, either zip your file or change the extentsion to txt.
ga97cad
2021년 2월 2일
ga97cad
2021년 2월 2일
dpb
2021년 2월 2일
The file is badly-formed -- there are not the same number of elements in every record -- and it's not delimited but fixed width. What do you want done with the missing values?
>> fid=fopen('tex_file.txt','r'); % open a file handle
>> fgetl(fid); % skip header line
>> l1=fgetl(fid); % get first record
>> strfind(l1,'0.0') % locate the floating pt zero
ans =
23 29 36 316 323 330 337
>> V1=str2double(split(l1(23:end))); % decocde all values from there to end record
>> l2=fgetl(fid); % repeat above for next record
>> V2=str2double(split(l2(23:end)));
>> [V1(1:5) V2(1:5)].' % show we got the right values for beginning of records
ans =
0 0.0100 0.0500 0.1700 0.5400
0 5.7200 16.2100 30.8100 31.7500
>>
>> whos V1 V2 % and show what actually was in the two records
Name Size Bytes Class Attributes
V1 46x1 368 double
V2 30x1 240 double
>>
As the last shows, there are 16 more values in the first record than the second; it appears at a glance there are a whole lot of different records lengths scattered throughout the file. That's ok in a spreadsheet, "not so much" in an array. That's the root cause of the problem in importing the file as it is.
So,the Q? is, what is to be done about the missing values?
ga97cad
2021년 2월 2일
Adam Danz
2021년 2월 2일
I don't see the attachment.
ga97cad
2021년 2월 2일
ga97cad
2021년 2월 2일
채택된 답변
추가 답변 (1개)
The phone rang...and Chris got back first, but my next response was to be--
opt=detectImportOptions('tex_file.txt','FileType',"fixedwidth",'NumHeaderLines',1);
opt.VariableTypes(1)={'datetime'};
opt=setvaropts(opt,"Var1","InputFormat",'dd.MM.yyyy');
tA=readtable('tex_file.txt',opt);
tA.Date=tA.Var1+tA.Var2;
tA.Date.Format='default';
tA=[tA(:,end) tA(:,3:end-1)];
which leaves a table looking like:
>> head(tA)
ans =
8×48 table
Date Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var15 Var16 Var17 Var18 Var19 Var20 Var21 Var22 Var23 Var24 Var25 Var26 Var27 Var28 Var29 Var30 Var31 Var32 Var33 Var34 Var35 Var36 Var37 Var38 Var39 Var40 Var41 Var42 Var43 Var44 Var45 Var46 Var47 Var48 Var49
____________________ ____ ____ ____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____
28-Jan-2021 12:16:19 0 0 0.01 0.05 0.17 0.54 1.65 3.04 3.57 5.79 15.37 30.25 31.76 33.01 34 33.89 33.9 33.8 33.81 33.85 33.71 33.72 33.75 33.59 33.55 33.67 33.49 33.44 33.41 33.44 33.49 33.42 33.38 31.73 31.33 24.23 9.61 3.81 3.23 3.01 1.35 0.4 0.12 0.03 0.01 0.01 0
28-Jan-2021 12:16:25 0 0 5.72 16.21 30.81 31.75 33.08 33.99 33.95 33.88 33.87 33.87 33.91 33.75 33.73 33.79 33.63 33.63 33.67 33.52 33.41 33.46 33.52 33.61 33.47 33.33 31.68 31.12 22.89 9.15 2.91 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
28-Jan-2021 12:16:31 0 0 9.33 23.79 31.62 32.14 33.81 33.88 33.96 33.92 33.82 33.86 33.84 33.73 33.77 33.72 33.6 33.64 33.67 33.41 33.41 33.44 33.51 33.45 33.59 32.43 31.33 29.89 15.59 6.02 3.47 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
28-Jan-2021 12:16:37 0 0 0.81 2.16 3.09 3.69 7.04 19.71 30.89 31.98 33.45 33.84 33.93 33.87 33.78 33.86 33.92 33.79 33.71 33.74 33.58 33.6 33.65 33.53 33.38 33.41 33.5 33.58 33.56 32.89 31.51 30.98 19.58 7.49 3.54 3.15 2.71 0.94 0.29 0.09 0.02 0.01 0.01 0 NaN NaN NaN
28-Jan-2021 12:16:44 0 0 0.01 0.04 0.16 0.51 1.55 3.03 3.52 5.6 16.26 30.6 31.71 32.86 33.99 33.94 33.87 33.85 33.83 33.91 33.74 33.69 33.78 33.63 33.59 33.64 33.52 33.4 33.39 33.47 33.51 33.47 33.33 31.67 31.24 24.68 9.9 3.47 3.22 3.04 1.43 0.41 0.12 0.03 0.01 0.01 0
28-Jan-2021 12:16:51 0 0 7.2 18.81 31.24 31.8 33.14 33.88 33.93 33.88 33.85 33.86 33.84 33.77 33.72 33.78 33.61 33.55 33.7 33.49 33.43 33.45 33.48 33.58 33.52 33.15 31.61 31.2 22.28 8.86 3.46 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
28-Jan-2021 12:16:56 0 0 9.35 24.71 31.66 32.1 33.79 33.78 33.92 33.83 33.85 33.84 33.81 33.69 33.75 33.74 33.54 33.65 33.6 33.42 33.39 33.39 33.5 33.51 33.63 32.31 31.36 29.94 15.75 6.07 3.49 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
28-Jan-2021 12:17:02 0 0 0.81 2.17 3.1 3.68 6.99 20.66 31.34 31.97 33.47 33.95 33.95 33.87 33.87 33.88 33.83 33.76 33.75 33.73 33.57 33.59 33.65 33.5 33.38 33.46 33.52 33.53 33.57 32.83 31.54 30.69 19.86 8.06 3.12 3.15 2.71 0.96 0.29 0.09 0.03 0.01 0.01 0 NaN NaN NaN
>>
Then as Chris shows, you can
figure, surf(tA{:,2:end})
that gives as his

댓글 수: 6
Cris LaPierre
2021년 2월 2일
편집: Cris LaPierre
2021년 2월 3일
I got wrapped up making fixedWidthImportOptions work. This approach is much simpler. I'd even further simplify it to the following:
data = readtable("tex_file.dat",'FileType',"fixedwidth",'NumHeaderLines',1);
surf(data{:,3:end})
dpb
2021년 2월 2일
readtable internal parsing has been much improved, recently, it appears. I hadn't even thought to try it directly given previous experience wherein fixed-width files were completely broken prior to introduction of the fixedWidthImportOptions object. That's a major step forward.
I didn't think about setting the first field width to encompass both the date and time field as one; that's a good trick for future where the date field may well be significant as well although it appears it is superfluous here.
ga97cad
2021년 2월 2일
Mine's essentially identical to Chris' with the fixedWidthImportObject except I didn't think to combine the date and time fields into one field as he did but converted the resulting two variables of date and time into one and then deleted the two superfluous ones.
You virtually never need loops in MATLAB and you can easily get the minutes from the combined datetime values with minute function--with the table I had above, using just the initial eight rows the head function returns for easy demo:
>> head(tA)
ans =
8×48 table
Date Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var15 Var16 Var17 Var18 Var19 Var20 Var21 Var22 Var23 Var24 Var25 Var26 Var27 Var28 Var29 Var30 Var31 Var32 Var33 Var34 Var35 Var36 Var37 Var38 Var39 Var40 Var41 Var42 Var43 Var44 Var45 Var46 Var47 Var48 Var49
____________________ ____ ____ ____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____
28-Jan-2021 12:16:19 0 0 0.01 0.05 0.17 0.54 1.65 3.04 3.57 5.79 15.37 30.25 31.76 33.01 34 33.89 33.9 33.8 33.81 33.85 33.71 33.72 33.75 33.59 33.55 33.67 33.49 33.44 33.41 33.44 33.49 33.42 33.38 31.73 31.33 24.23 9.61 3.81 3.23 3.01 1.35 0.4 0.12 0.03 0.01 0.01 0
28-Jan-2021 12:16:25 0 0 5.72 16.21 30.81 31.75 33.08 33.99 33.95 33.88 33.87 33.87 33.91 33.75 33.73 33.79 33.63 33.63 33.67 33.52 33.41 33.46 33.52 33.61 33.47 33.33 31.68 31.12 22.89 9.15 2.91 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
28-Jan-2021 12:16:31 0 0 9.33 23.79 31.62 32.14 33.81 33.88 33.96 33.92 33.82 33.86 33.84 33.73 33.77 33.72 33.6 33.64 33.67 33.41 33.41 33.44 33.51 33.45 33.59 32.43 31.33 29.89 15.59 6.02 3.47 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
28-Jan-2021 12:16:37 0 0 0.81 2.16 3.09 3.69 7.04 19.71 30.89 31.98 33.45 33.84 33.93 33.87 33.78 33.86 33.92 33.79 33.71 33.74 33.58 33.6 33.65 33.53 33.38 33.41 33.5 33.58 33.56 32.89 31.51 30.98 19.58 7.49 3.54 3.15 2.71 0.94 0.29 0.09 0.02 0.01 0.01 0 NaN NaN NaN
28-Jan-2021 12:16:44 0 0 0.01 0.04 0.16 0.51 1.55 3.03 3.52 5.6 16.26 30.6 31.71 32.86 33.99 33.94 33.87 33.85 33.83 33.91 33.74 33.69 33.78 33.63 33.59 33.64 33.52 33.4 33.39 33.47 33.51 33.47 33.33 31.67 31.24 24.68 9.9 3.47 3.22 3.04 1.43 0.41 0.12 0.03 0.01 0.01 0
28-Jan-2021 12:16:51 0 0 7.2 18.81 31.24 31.8 33.14 33.88 33.93 33.88 33.85 33.86 33.84 33.77 33.72 33.78 33.61 33.55 33.7 33.49 33.43 33.45 33.48 33.58 33.52 33.15 31.61 31.2 22.28 8.86 3.46 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
28-Jan-2021 12:16:56 0 0 9.35 24.71 31.66 32.1 33.79 33.78 33.92 33.83 33.85 33.84 33.81 33.69 33.75 33.74 33.54 33.65 33.6 33.42 33.39 33.39 33.5 33.51 33.63 32.31 31.36 29.94 15.75 6.07 3.49 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
28-Jan-2021 12:17:02 0 0 0.81 2.17 3.1 3.68 6.99 20.66 31.34 31.97 33.47 33.95 33.95 33.87 33.87 33.88 33.83 33.76 33.75 33.73 33.57 33.59 33.65 33.5 33.38 33.46 33.52 33.53 33.57 32.83 31.54 30.69 19.86 8.06 3.12 3.15 2.71 0.96 0.29 0.09 0.03 0.01 0.01 0 NaN NaN NaN
>> minute(ans.Date)
ans =
16
16
16
16
16
16
16
17
>>
NB: Above at command line the variable ans is the table consisting of the first eight rows of tA; for the whole table just return
mins=minute(tA.Date);
and you've got the full array of the minutes. There's no need to keep time separate from date or extract subsections of a string time or anything such as that. The datetime class is full-fledged class with methods and functions to do anything needed with it.
ga97cad
2021년 2월 3일
" i needet to substrate the first row from every of the following rows in order so start from zero,..."
Then just do so...
tA.ETime=tA.Date-tA.Date(1); % create duration variable since first sample
tA=tA(:,[1 end 2:end-1]); % rearrange put new column 2nd after .Date.
leaving
>> head(tA)
ans =
8×49 table
Date ETime Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var15 Var16 Var17 Var18 Var19 Var20 Var21 Var22 Var23 Var24 Var25 Var26 Var27 Var28 Var29 Var30 Var31 Var32 Var33 Var34 Var35 Var36 Var37 Var38 Var39 Var40 Var41 Var42 Var43 Var44 Var45 Var46 Var47 Var48 Var49
____________________ ________ ____ ____ ____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____
28-Jan-2021 12:16:19 00:00:00 0 0 0.01 0.05 0.17 0.54 1.65 3.04 3.57 5.79 15.37 30.25 31.76 33.01 34 33.89 33.9 33.8 33.81 33.85 33.71 33.72 33.75 33.59 33.55 33.67 33.49 33.44 33.41 33.44 33.49 33.42 33.38 31.73 31.33 24.23 9.61 3.81 3.23 3.01 1.35 0.4 0.12 0.03 0.01 0.01 0
28-Jan-2021 12:16:25 00:00:06 0 0 5.72 16.21 30.81 31.75 33.08 33.99 33.95 33.88 33.87 33.87 33.91 33.75 33.73 33.79 33.63 33.63 33.67 33.52 33.41 33.46 33.52 33.61 33.47 33.33 31.68 31.12 22.89 9.15 2.91 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
28-Jan-2021 12:16:31 00:00:12 0 0 9.33 23.79 31.62 32.14 33.81 33.88 33.96 33.92 33.82 33.86 33.84 33.73 33.77 33.72 33.6 33.64 33.67 33.41 33.41 33.44 33.51 33.45 33.59 32.43 31.33 29.89 15.59 6.02 3.47 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
28-Jan-2021 12:16:37 00:00:18 0 0 0.81 2.16 3.09 3.69 7.04 19.71 30.89 31.98 33.45 33.84 33.93 33.87 33.78 33.86 33.92 33.79 33.71 33.74 33.58 33.6 33.65 33.53 33.38 33.41 33.5 33.58 33.56 32.89 31.51 30.98 19.58 7.49 3.54 3.15 2.71 0.94 0.29 0.09 0.02 0.01 0.01 0 NaN NaN NaN
28-Jan-2021 12:16:44 00:00:25 0 0 0.01 0.04 0.16 0.51 1.55 3.03 3.52 5.6 16.26 30.6 31.71 32.86 33.99 33.94 33.87 33.85 33.83 33.91 33.74 33.69 33.78 33.63 33.59 33.64 33.52 33.4 33.39 33.47 33.51 33.47 33.33 31.67 31.24 24.68 9.9 3.47 3.22 3.04 1.43 0.41 0.12 0.03 0.01 0.01 0
28-Jan-2021 12:16:51 00:00:32 0 0 7.2 18.81 31.24 31.8 33.14 33.88 33.93 33.88 33.85 33.86 33.84 33.77 33.72 33.78 33.61 33.55 33.7 33.49 33.43 33.45 33.48 33.58 33.52 33.15 31.61 31.2 22.28 8.86 3.46 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
28-Jan-2021 12:16:56 00:00:37 0 0 9.35 24.71 31.66 32.1 33.79 33.78 33.92 33.83 33.85 33.84 33.81 33.69 33.75 33.74 33.54 33.65 33.6 33.42 33.39 33.39 33.5 33.51 33.63 32.31 31.36 29.94 15.75 6.07 3.49 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
28-Jan-2021 12:17:02 00:00:43 0 0 0.81 2.17 3.1 3.68 6.99 20.66 31.34 31.97 33.47 33.95 33.95 33.87 33.87 33.88 33.83 33.76 33.75 33.73 33.57 33.59 33.65 33.5 33.38 33.46 33.52 33.53 33.57 32.83 31.54 30.69 19.86 8.06 3.12 3.15 2.71 0.96 0.29 0.09 0.03 0.01 0.01 0 NaN NaN NaN
>>
If you want it displayed as fractional minutes, just ask/say so... :)
>> tA.ETime.Format='m';
>> head(tA)
ans =
8×49 table
Date ETime Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var15 Var16 Var17 Var18 Var19 Var20 Var21 Var22 Var23 Var24 Var25 Var26 Var27 Var28 Var29 Var30 Var31 Var32 Var33 Var34 Var35 Var36 Var37 Var38 Var39 Var40 Var41 Var42 Var43 Var44 Var45 Var46 Var47 Var48 Var49
____________________ ___________ ____ ____ ____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____
28-Jan-2021 12:16:19 0 min 0 0 0.01 0.05 0.17 0.54 1.65 3.04 3.57 5.79 15.37 30.25 31.76 33.01 34 33.89 33.9 33.8 33.81 33.85 33.71 33.72 33.75 33.59 33.55 33.67 33.49 33.44 33.41 33.44 33.49 33.42 33.38 31.73 31.33 24.23 9.61 3.81 3.23 3.01 1.35 0.4 0.12 0.03 0.01 0.01 0
28-Jan-2021 12:16:25 0.1 min 0 0 5.72 16.21 30.81 31.75 33.08 33.99 33.95 33.88 33.87 33.87 33.91 33.75 33.73 33.79 33.63 33.63 33.67 33.52 33.41 33.46 33.52 33.61 33.47 33.33 31.68 31.12 22.89 9.15 2.91 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
28-Jan-2021 12:16:31 0.2 min 0 0 9.33 23.79 31.62 32.14 33.81 33.88 33.96 33.92 33.82 33.86 33.84 33.73 33.77 33.72 33.6 33.64 33.67 33.41 33.41 33.44 33.51 33.45 33.59 32.43 31.33 29.89 15.59 6.02 3.47 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
28-Jan-2021 12:16:37 0.3 min 0 0 0.81 2.16 3.09 3.69 7.04 19.71 30.89 31.98 33.45 33.84 33.93 33.87 33.78 33.86 33.92 33.79 33.71 33.74 33.58 33.6 33.65 33.53 33.38 33.41 33.5 33.58 33.56 32.89 31.51 30.98 19.58 7.49 3.54 3.15 2.71 0.94 0.29 0.09 0.02 0.01 0.01 0 NaN NaN NaN
28-Jan-2021 12:16:44 0.41667 min 0 0 0.01 0.04 0.16 0.51 1.55 3.03 3.52 5.6 16.26 30.6 31.71 32.86 33.99 33.94 33.87 33.85 33.83 33.91 33.74 33.69 33.78 33.63 33.59 33.64 33.52 33.4 33.39 33.47 33.51 33.47 33.33 31.67 31.24 24.68 9.9 3.47 3.22 3.04 1.43 0.41 0.12 0.03 0.01 0.01 0
28-Jan-2021 12:16:51 0.53333 min 0 0 7.2 18.81 31.24 31.8 33.14 33.88 33.93 33.88 33.85 33.86 33.84 33.77 33.72 33.78 33.61 33.55 33.7 33.49 33.43 33.45 33.48 33.58 33.52 33.15 31.61 31.2 22.28 8.86 3.46 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
28-Jan-2021 12:16:56 0.61667 min 0 0 9.35 24.71 31.66 32.1 33.79 33.78 33.92 33.83 33.85 33.84 33.81 33.69 33.75 33.74 33.54 33.65 33.6 33.42 33.39 33.39 33.5 33.51 33.63 32.31 31.36 29.94 15.75 6.07 3.49 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
28-Jan-2021 12:17:02 0.71667 min 0 0 0.81 2.17 3.1 3.68 6.99 20.66 31.34 31.97 33.47 33.95 33.95 33.87 33.87 33.88 33.83 33.76 33.75 33.73 33.57 33.59 33.65 33.5 33.38 33.46 33.52 33.53 33.57 32.83 31.54 30.69 19.86 8.06 3.12 3.15 2.71 0.96 0.29 0.09 0.03 0.01 0.01 0 NaN NaN NaN
>>
plot and friends are datetime, duration-aware, so plotting and so on is transparent. About the only place you might have to actually use the elapsed or calendar time as a double array would be if were to use as an independent variable in a curvefit or such; those analysis tools aren't all overloaded for non-numeric variables (other than some can handle categorical variables).
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
