Reading data in MATLAB
20 次查看(过去 30 天)
显示 更早的评论
Hello,
just wanted to plot data from .txt-file as below:
T = readtable('data_Labor1_CH1_AnAusSpannung.txt','VariableNamingRule','preserve')
t = T.("Time(NL3)");
U = T.("Voltage(NL3)")
figure()
plot(t,U,'b','LineWidth',2),hold on, grid on;
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.
0 个评论
回答(2 个)
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 = T.("Time(NL3)")
U = T.("Voltage(NL3)")
t = strrep(t, ',','.');
t = strrep(t, '10^','E');
t = str2double(cellfun(@(x)sscanf(x, '%s \\cdot %s'), t, 'Unif',0))
U = strrep(U, ',','.');
U = strrep(U, '10^','E');
U = str2double(cellfun(@(x)sscanf(x, '%s \\cdot %s'), U, 'Unif',0))
figure()
plot(t,U,'b','LineWidth',2),hold on, grid on;
.
2 个评论
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')
% 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)
% Convert table variables to double
data = convertvars(data,@isstring,'double')
% Plot
t = data.("Time(NL3)");
U = data.("Voltage(NL3)");
figure()
plot(t,U,'b','LineWidth',2)
grid on;
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!