Plotting graph from.txt files and splitting their names

1 view (last 30 days)
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);

Answers (1)

Voss
Voss on 23 Apr 2024
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);

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!