Reading data in MATLAB
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Alisa-Oleksandra Kotliarova
el 12 de Nov. de 2024
Comentada: Star Strider
el 12 de Nov. de 2024
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 comentarios
Respuestas (2)
Star Strider
el 12 de Nov. de 2024
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 comentarios
Cris LaPierre
el 12 de Nov. de 2024
Editada: Cris LaPierre
el 12 de Nov. de 2024
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;
Ver también
Categorías
Más información sobre Annotations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!