Reading data in MATLAB
3 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Star Strider
on 12 Nov 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 Comments
Cris LaPierre
on 12 Nov 2024
Edited: Cris LaPierre
on 12 Nov 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;
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!