Reading data in MATLAB
20 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Alisa-Oleksandra Kotliarova
le 12 Nov 2024 à 13:54
Commenté : Star Strider
le 12 Nov 2024 à 22:31
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 commentaires
Réponses (2)
Star Strider
le 12 Nov 2024 à 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 commentaires
Cris LaPierre
le 12 Nov 2024 à 14:49
Modifié(e) : Cris LaPierre
le 12 Nov 2024 à 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;
Voir également
Catégories
En savoir plus sur Annotations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!