Plotting graph from.txt files and splitting their names

1 次查看(过去 30 天)
Hello,
How can I read multiple files named like these (attached), separate them by names and plot one graph?
I've tried using this code below, however my graph appears blank but with any error message. I tried to fix it in different ways but I couldnt. Can someone give me a hand on it?
dir_files = 'Bending';
F = dir(fullfile(dir_files,'*.txt'));
colormatrix = [0.61,0.61,0.68; 0,1,1; 0,0,1];
mmatrix = ['o'; '*'; '+';'.'];
for ii = 1:numel(F)
newStr = split(F(ii).name,'-')
switch newStr(1)
case 'X'
color1 = colormatrix(1,:);
case 'N'
color1 = colormatrix(2,:);
end
hold on
switch newStr(3)
case 'before'
m1 = mmatrix(1,:);
case 'during'
m1 = mmatrix(3,:);
end
hold on
F(ii).Data = readtable(fullfile(F(ii).folder,F(ii).name),'DecimalSeparator',',');
plot(F(ii).Data.T,F(ii).Data.Vf, Marker=mmatrix, MarkerSize=12, Color=color1, DisplayName=F(ii).name)
end
legend({F.name},'Location','eastoutside')
set(legend, 'FontSize',12);
xlabel('Time, t [s]');
ylabel('Potential, E [V]');
title('OCP monitoring');
colormap(colormatrix);
c = colorbar('southoutside', 'Ticks' ,[0.17,0.50,0.85], ...
'TickLabels',{'0.5' , '16' , '22'});
c.Label.String = 'Immersion time (h)';
set(c, 'FontSize',10);

回答(1 个)

Voss
Voss 2024-4-23
dir_files = '.';
F = dir(fullfile(dir_files,'*.txt'));
colormatrix = [0.61,0.61,0.68; 0,1,1; 0,0,1];
mmatrix = ['o'; '*'; '+';'.'];
for ii = 1:numel(F)
newStr = split(F(ii).name,'-')
switch newStr{1} % <- modified
case 'X'
color1 = colormatrix(1,:);
case 'N'
color1 = colormatrix(2,:);
end
hold on
switch newStr{3} % <- modified
case 'before.txt' % <- modified
m1 = mmatrix(1,:);
case 'during.txt' % <- modified
m1 = mmatrix(3,:);
end
hold on
F(ii).Data = readtable(fullfile(F(ii).folder,F(ii).name),'DecimalSeparator',',');
plot(F(ii).Data.T,F(ii).Data.Vf, Marker=m1, MarkerSize=12, Color=color1, DisplayName=F(ii).name)
% ^^ modified
end
newStr = 3x1 cell array
{'N' } {'4A' } {'before.txt'}
newStr = 3x1 cell array
{'X' } {'4A' } {'during.txt'}
legend({F.name},'Location','eastoutside')
set(legend, 'FontSize',12);
xlabel('Time, t [s]');
ylabel('Potential, E [V]');
title('OCP monitoring');
colormap(colormatrix);
c = colorbar('southoutside', 'Ticks' ,[0.17,0.50,0.85], ...
'TickLabels',{'0.5' , '16' , '22'});
c.Label.String = 'Immersion time (h)';
set(c, 'FontSize',10);

类别

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

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by