Convert Table to Matrix

105 vues (au cours des 30 derniers jours)
ga97cad
ga97cad le 2 Fév 2021
Modifié(e) : dpb le 3 Fév 2021
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 commentaires
ga97cad
ga97cad le 2 Fév 2021
submitted my question while your were uploading your's obviously... oh my god.... that looks really complicated! thanks for putting so much effort in for helping out! I much appreciate it!!! The data with the missing values shall be ignored, that plot should in the end look like the file i have attached to the answer (with the excel prepared data).
I never thought that this would be sooo comlicated with matlab, as it is so easy to plot everything with the prepared excel data.
Is there no way to change the double space in the file with an semicolon via writematrix? and then read out the overwritten matrix with the inserted semicolons via readmatrix or readtable?
best reagrds, lukas
ga97cad
ga97cad le 2 Fév 2021
the mentioned prepared excel file looks like the file I have attached to the answer, and reading it out with readtable(filename) works perfectly fine. That is why I am asking how I
  1. either change the two spaces into a smicolon (writematrix) and then read it out
  2. or treat the doublespace as the delimintor and then read it out
thanks, Lukas

Connectez-vous pour commenter.

Réponse acceptée

Cris LaPierre
Cris LaPierre le 2 Fév 2021
Modifié(e) : Cris LaPierre le 2 Fév 2021
My suggestion would be to use the Import Tool. This will allow you to interactively create your import. There is an option to generate an import script or function as well.
Along the way, you get to inspect your data. In particular, you will notice that you have an inconsistent number of columns of data. This is going to possibly complicate creating a surface from the data, though not necessarily. The shorter rows will be padded with NaN.
I was able to successfully import the data using both the Import Tool and fixedWidthImportOptions.
numVars = 48;
opts = fixedWidthImportOptions("NumVariables",numVars,...
"DataLines",2);
opts = setvartype(opts,1,"datetime");
opts = setvartype(opts,2:numVars,"double");
opts = setvaropts(opts,1,"InputFormat","dd.MM.yyyy HH:mm:ss");
opts.VariableWidths = [19 2 4 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7];
data = readtable("tex_file.dat",opts)
From the the table, you could create a surf using the following code. For additional details on accessing data in a table, see this page.
surf(data{:,2:end})
  1 commentaire
ga97cad
ga97cad le 2 Fév 2021
Wow! It worked! Thank you so much for your help, Chris!!! Wouldn't have been able to find a solution like that within 50 years...
best regards, Lukas

Connectez-vous pour commenter.

Plus de réponses (1)

dpb
dpb le 2 Fév 2021
Modifié(e) : dpb le 2 Fév 2021
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 commentaires
ga97cad
ga97cad le 3 Fév 2021
thank you, @dpb, but i needet to substrate the first row from every of the following rows in order so start from zero, but the the for loop really wasn't that kind of a big deal! I applied minutes() and then it was just straightforward!
btw: the help that the two of you provided also helped me to solve another problem: I get data from a sensor with is separated by a comma instead of a dot (german programme und syntax), so matlab wasn't able to recognize it as a number and just printed NaN. I wasn't able to understand the solution that are provided in the forum so far, but now everything works perfectly fine!
Best regards, Lukas
dpb
dpb le 3 Fév 2021
Modifié(e) : dpb le 3 Fév 2021
" 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).

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by