Reading data in MATLAB

20 次查看(过去 30 天)
Alisa-Oleksandra Kotliarova
Alisa-Oleksandra Kotliarova 2024-11-12,13:54
评论: Star Strider 2024-11-12,22:31
Hello,
just wanted to plot data from .txt-file as below:
T = readtable('data_Labor1_CH1_AnAusSpannung.txt','VariableNamingRule','preserve')
T = 1200x3 table
NO. Voltage(NL3) Time(NL3) ___ ____________________ ______________________ 1 {'-9,60 \cdot 10^3'} {'-1,1980 \cdot 10^2'} 2 {'-1,12 \cdot 10^2'} {'-1,1960 \cdot 10^2'} 3 {'-9,60 \cdot 10^3'} {'-1,1940 \cdot 10^2'} 4 {'-9,60 \cdot 10^3'} {'-1,1920 \cdot 10^2'} 5 {'-9,60 \cdot 10^3'} {'-1,1900 \cdot 10^2'} 6 {'-9,60 \cdot 10^3'} {'-1,1880 \cdot 10^2'} 7 {'-9,60 \cdot 10^3'} {'-1,1860 \cdot 10^2'} 8 {'-9,60 \cdot 10^3'} {'-1,1840 \cdot 10^2'} 9 {'-9,60 \cdot 10^3'} {'-1,1820 \cdot 10^2'} 10 {'-1,04 \cdot 10^2'} {'-1,1800 \cdot 10^2'} 11 {'-1,04 \cdot 10^2'} {'-1,1780 \cdot 10^2'} 12 {'-1,12 \cdot 10^2'} {'-1,1760 \cdot 10^2'} 13 {'-9,60 \cdot 10^3'} {'-1,1740 \cdot 10^2'} 14 {'-1,04 \cdot 10^2'} {'-1,1720 \cdot 10^2'} 15 {'-9,60 \cdot 10^3'} {'-1,1700 \cdot 10^2'} 16 {'-1,04 \cdot 10^2'} {'-1,1680 \cdot 10^2'}
t = T.("Time(NL3)");
U = T.("Voltage(NL3)")
U = 1200x1 cell array
{'-9,60 \cdot 10^3'} {'-1,12 \cdot 10^2'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-1,04 \cdot 10^2'} {'-1,04 \cdot 10^2'} {'-1,12 \cdot 10^2'} {'-9,60 \cdot 10^3'} {'-1,04 \cdot 10^2'} {'-9,60 \cdot 10^3'} {'-1,04 \cdot 10^2'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-1,04 \cdot 10^2'} {'-1,04 \cdot 10^2'} {'-1,04 \cdot 10^2'} {'-1,04 \cdot 10^2'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-8,80 \cdot 10^3'} {'-8,80 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'}
figure()
plot(t,U,'b','LineWidth',2),hold on, grid on;
Error using plot
Invalid data argument.
Then it goes:
Error using plot
Invalid data argument.
Error in Untitled (line 7)
plot(t,U,'b','LineWidth',2),hold on, grid on;
What could I do wrong? Thanks.

回答(2 个)

Star Strider
Star Strider 2024-11-12,14:11
Try this —
% T = readtable('data_Labor1_CH1_AnAusSpannung.txt','VariableNamingRule','preserve')
T = readtable('data_Labor1_CH...sSpannung.txt','VariableNamingRule','preserve')
T = 1200x3 table
NO. Voltage(NL3) Time(NL3) ___ ____________________ ______________________ 1 {'-9,60 \cdot 10^3'} {'-1,1980 \cdot 10^2'} 2 {'-1,12 \cdot 10^2'} {'-1,1960 \cdot 10^2'} 3 {'-9,60 \cdot 10^3'} {'-1,1940 \cdot 10^2'} 4 {'-9,60 \cdot 10^3'} {'-1,1920 \cdot 10^2'} 5 {'-9,60 \cdot 10^3'} {'-1,1900 \cdot 10^2'} 6 {'-9,60 \cdot 10^3'} {'-1,1880 \cdot 10^2'} 7 {'-9,60 \cdot 10^3'} {'-1,1860 \cdot 10^2'} 8 {'-9,60 \cdot 10^3'} {'-1,1840 \cdot 10^2'} 9 {'-9,60 \cdot 10^3'} {'-1,1820 \cdot 10^2'} 10 {'-1,04 \cdot 10^2'} {'-1,1800 \cdot 10^2'} 11 {'-1,04 \cdot 10^2'} {'-1,1780 \cdot 10^2'} 12 {'-1,12 \cdot 10^2'} {'-1,1760 \cdot 10^2'} 13 {'-9,60 \cdot 10^3'} {'-1,1740 \cdot 10^2'} 14 {'-1,04 \cdot 10^2'} {'-1,1720 \cdot 10^2'} 15 {'-9,60 \cdot 10^3'} {'-1,1700 \cdot 10^2'} 16 {'-1,04 \cdot 10^2'} {'-1,1680 \cdot 10^2'}
t = T.("Time(NL3)")
t = 1200x1 cell array
{'-1,1980 \cdot 10^2'} {'-1,1960 \cdot 10^2'} {'-1,1940 \cdot 10^2'} {'-1,1920 \cdot 10^2'} {'-1,1900 \cdot 10^2'} {'-1,1880 \cdot 10^2'} {'-1,1860 \cdot 10^2'} {'-1,1840 \cdot 10^2'} {'-1,1820 \cdot 10^2'} {'-1,1800 \cdot 10^2'} {'-1,1780 \cdot 10^2'} {'-1,1760 \cdot 10^2'} {'-1,1740 \cdot 10^2'} {'-1,1720 \cdot 10^2'} {'-1,1700 \cdot 10^2'} {'-1,1680 \cdot 10^2'} {'-1,1660 \cdot 10^2'} {'-1,1640 \cdot 10^2'} {'-1,1620 \cdot 10^2'} {'-1,1600 \cdot 10^2'} {'-1,1580 \cdot 10^2'} {'-1,1560 \cdot 10^2'} {'-1,1540 \cdot 10^2'} {'-1,1520 \cdot 10^2'} {'-1,1500 \cdot 10^2'} {'-1,1480 \cdot 10^2'} {'-1,1460 \cdot 10^2'} {'-1,1440 \cdot 10^2'} {'-1,1420 \cdot 10^2'} {'-1,1400 \cdot 10^2'}
U = T.("Voltage(NL3)")
U = 1200x1 cell array
{'-9,60 \cdot 10^3'} {'-1,12 \cdot 10^2'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-1,04 \cdot 10^2'} {'-1,04 \cdot 10^2'} {'-1,12 \cdot 10^2'} {'-9,60 \cdot 10^3'} {'-1,04 \cdot 10^2'} {'-9,60 \cdot 10^3'} {'-1,04 \cdot 10^2'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-1,04 \cdot 10^2'} {'-1,04 \cdot 10^2'} {'-1,04 \cdot 10^2'} {'-1,04 \cdot 10^2'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-8,80 \cdot 10^3'} {'-8,80 \cdot 10^3'} {'-9,60 \cdot 10^3'} {'-9,60 \cdot 10^3'}
t = strrep(t, ',','.');
t = strrep(t, '10^','E');
t = str2double(cellfun(@(x)sscanf(x, '%s \\cdot %s'), t, 'Unif',0))
t = 1200×1
-119.8000 -119.6000 -119.4000 -119.2000 -119.0000 -118.8000 -118.6000 -118.4000 -118.2000 -118.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
U = strrep(U, ',','.');
U = strrep(U, '10^','E');
U = str2double(cellfun(@(x)sscanf(x, '%s \\cdot %s'), U, 'Unif',0))
U = 1200×1
-9600 -112 -9600 -9600 -9600 -9600 -9600 -9600 -9600 -104
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
figure()
plot(t,U,'b','LineWidth',2),hold on, grid on;
.

Cris LaPierre
Cris LaPierre 2024-11-12,14:49
编辑:Cris LaPierre 2024-11-12,14:57
The numbers are not captured in a format MATLAB can interpret. I would apply post-processing to turn the captured char arrays to a number format recognized by MATLAB.
file = 'data_Labor1_CH1_AnAusSpannung.txt';
data = readtable(file,'VariableNamingRule','preserve')
data = 1200x3 table
NO. Voltage(NL3) Time(NL3) ___ ____________________ ______________________ 1 {'-9,60 \cdot 10^3'} {'-1,1980 \cdot 10^2'} 2 {'-1,12 \cdot 10^2'} {'-1,1960 \cdot 10^2'} 3 {'-9,60 \cdot 10^3'} {'-1,1940 \cdot 10^2'} 4 {'-9,60 \cdot 10^3'} {'-1,1920 \cdot 10^2'} 5 {'-9,60 \cdot 10^3'} {'-1,1900 \cdot 10^2'} 6 {'-9,60 \cdot 10^3'} {'-1,1880 \cdot 10^2'} 7 {'-9,60 \cdot 10^3'} {'-1,1860 \cdot 10^2'} 8 {'-9,60 \cdot 10^3'} {'-1,1840 \cdot 10^2'} 9 {'-9,60 \cdot 10^3'} {'-1,1820 \cdot 10^2'} 10 {'-1,04 \cdot 10^2'} {'-1,1800 \cdot 10^2'} 11 {'-1,04 \cdot 10^2'} {'-1,1780 \cdot 10^2'} 12 {'-1,12 \cdot 10^2'} {'-1,1760 \cdot 10^2'} 13 {'-9,60 \cdot 10^3'} {'-1,1740 \cdot 10^2'} 14 {'-1,04 \cdot 10^2'} {'-1,1720 \cdot 10^2'} 15 {'-9,60 \cdot 10^3'} {'-1,1700 \cdot 10^2'} 16 {'-1,04 \cdot 10^2'} {'-1,1680 \cdot 10^2'}
% I find strings are easier to work with
data = convertvars(data,@iscell,'string');
% replace the expression with scientific notation (replaces the decimal separator, too)
clnFxn = @(x) str2double(replace(replace(x,' \cdot 10^','E'),',','.'));
data(:,vartype('string')) = varfun(clnFxn,data,'InputVariables',@isstring)
data = 1200x3 table
NO. Voltage(NL3) Time(NL3) ___ ____________ _________ 1 "-9600" "-119.8" 2 "-112" "-119.6" 3 "-9600" "-119.4" 4 "-9600" "-119.2" 5 "-9600" "-119" 6 "-9600" "-118.8" 7 "-9600" "-118.6" 8 "-9600" "-118.4" 9 "-9600" "-118.2" 10 "-104" "-118" 11 "-104" "-117.8" 12 "-112" "-117.6" 13 "-9600" "-117.4" 14 "-104" "-117.2" 15 "-9600" "-117" 16 "-104" "-116.8"
% Convert table variables to double
data = convertvars(data,@isstring,'double')
data = 1200x3 table
NO. Voltage(NL3) Time(NL3) ___ ____________ _________ 1 -9600 -119.8 2 -112 -119.6 3 -9600 -119.4 4 -9600 -119.2 5 -9600 -119 6 -9600 -118.8 7 -9600 -118.6 8 -9600 -118.4 9 -9600 -118.2 10 -104 -118 11 -104 -117.8 12 -112 -117.6 13 -9600 -117.4 14 -104 -117.2 15 -9600 -117 16 -104 -116.8
% Plot
t = data.("Time(NL3)");
U = data.("Voltage(NL3)");
figure()
plot(t,U,'b','LineWidth',2)
grid on;

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by