how can i plot many series of values in the same plot

i have a series of txt files which i need to use to make one single plot with all of them in it but this command seems not to work properly.
i'm kinda new to matlab so a friend tried to help me out with this one but i don't know how to make it work.
it gives me an error on the first line
%% Import single I-V
clear, clc; close all; % clear variables and cmd window, close figures
% Import raw measurement .epr file
[FileName,FilePath] = uigetfile('*.txt','Choose a measurement');
File = [FilePath, FileName];
data = readmatrix(File,'Range','A1:B70','FileType','delimitedtext');
V = data(:,1);
I = data(:,2);
%% Plot single I-V
figure(1);
plot(V,I,'- .','DisplayName',FileName)
% semilogy(V,abs(I),'- .','DisplayName',FileName)
xlabel('Voltage (V)')
ylabel('Current (mA)')
axis tight
xline(0,'HandleVisibility','off')
yline(0,'HandleVisibility','off')
grid on
box on
legend('Location','northwest','FontSize',8,'Interpreter','None')
pbaspect([1.25 1 1])
%% Import all I-V
% Clear all variables, close all figures, clear command window
clear; close all; clc;
% Navigate Current Folder to folder containing all files
% Get all .epr files in current directory
txt_files = dir('*.txt');
% Initialise empty cell array to store data
data = cell(0);
% Loop through each csv file to read data from specific rows and columns
for i = 1:numel(txt_files)
data{i} = readmatrix(txt_files(i).name,'Range','A1:B70','FileType','delimitedtext');
end
%% Plot all I-V
for i = 1:numel(txt_files)
V = data{1, i}(:,1);
I = data{1, i}(:,2);
figure(1);
plot(V,I,'- .','DisplayName',txt_files(i).name)
% semilogy(V,abs(I),'- .','DisplayName',txt_files(i).name)
hold on
end
hold off
xlabel('Voltage (V)')
ylabel('Current (mA)')
axis tight
%xline(0,'HandleVisibility','off')
%yline(0,'HandleVisibility','off')
grid on
box on
legend('Location','northwest','FontSize',8,'Interpreter','None')
pbaspect([1.25 1 1])

2 Comments

What is the error?
It works for me (with an arbitrary file) through ‘figure(1)’. (I don’t have the other files so I didn’t run the rest of it.)
i don't know how for other codes like this works but it plots only the selected one, like i said it gives error but i do not see where it can be.
do you happen to have a simpler code since i'm still new to the platform?
ex of code working but only plots one:
clear
%Importare dati, indicare percorso file dati completo
load ST_n_293_testo.txt;
xdata=ST_n_293_testo(:,1); % v (riga, colonna)
ydata=log(ST_n_293_testo(:,2)); % i (riga, colonna)
%plot dati
plot(xdata,ydata,'ro');
title('I-V @ 293K')
xlabel('V [V]');
ylabel('I [mA]');

Sign in to comment.

 Accepted Answer

Looking a bit more carefully at your posted code, I believe the problem is that you are creating a new figure in each loop iteration.
Try this instead:
figure(1);
for i = 1:numel(txt_files)
V = data{1, i}(:,1);
I = data{1, i}(:,2);
plot(V,I,'- .','DisplayName',txt_files(i).name)
% semilogy(V,abs(I),'- .','DisplayName',txt_files(i).name)
hold on
end
I could not run the code, so I did not see that before.
.

6 Comments

i'll try in a bit, thank you in advance.
I'll keep you updated if u don't mind.
My pleasure!
I don’t mind at all!
.
i can't seem to be using it right, and i don't know what i'm doing wrong at this point, these are the files i'm currently trying to plot and after this i have to fit them.
i'm totally clueless at this point, considering the fact that i haven't even used matlab before
This seems to work correctly here (R2023a).
What do you want your code to do?
% Import raw measurement .epr file
% [FileName,FilePath] = uigetfile('*.txt','Choose a measurement');
Files = dir('*.txt')
Files = 15×1 struct array with fields:
name folder date bytes isdir datenum
FileName = Files(1).name
FileName = 'ST_n_100_testo.txt'
File = FileName
File = 'ST_n_100_testo.txt'
% File = [FilePath, FileName];
data = readmatrix(File,'Range','A1:B70','FileType','delimitedtext');
V = data(:,1);
I = data(:,2);
%% Plot single I-V
figure(1);
plot(V,I,'- .','DisplayName',FileName)
% semilogy(V,abs(I),'- .','DisplayName',FileName)
xlabel('Voltage (V)')
ylabel('Current (mA)')
axis tight
xline(0,'HandleVisibility','off')
yline(0,'HandleVisibility','off')
grid on
box on
legend('Location','northwest','FontSize',8,'Interpreter','None')
pbaspect([1.25 1 1])
%% Import all I-V
% Clear all variables, close all figures, clear command window
clear; close all; clc;
% Navigate Current Folder to folder containing all files
% Get all .epr files in current directory
txt_files = dir('*.txt');
% Initialise empty cell array to store data
data = cell(0);
% Loop through each csv file to read data from specific rows and columns
for i = 1:numel(txt_files)
data{i} = readmatrix(txt_files(i).name,'Range','A1:B70','FileType','delimitedtext');
end
%% Plot all I-V
for i = 1:numel(txt_files)
V = data{1, i}(:,1);
I = data{1, i}(:,2);
figure(1);
plot(V,I,'- .','DisplayName',txt_files(i).name)
% semilogy(V,abs(I),'- .','DisplayName',txt_files(i).name)
hold on
end
hold off
xlabel('Voltage (V)')
ylabel('Current (mA)')
axis tight
%xline(0,'HandleVisibility','off')
%yline(0,'HandleVisibility','off')
grid on
box on
legend('Location','northwest','FontSize',8,'Interpreter','None')
pbaspect([1.25 1 1])
.
oh i see the problem was specifing the folder files, since i had to convert them from .ivc to .txt i had both copies in the same folders and mistakes were made while editing blanks spots.
thanks for the patience and the response.
As always, (our) pleasure!

Sign in to comment.

More Answers (1)

If you want multiple lines to appear with a single plot command, you need to be plotting a matrix. Each column of data is treated as a series.
X = 1:10;
% 3 columns so 3 lines
Y = rand(10,3);
plot(X,Y)
You can also use multple plot commands combined with a 'hold on'. Without it, each plot command replaces the previous plot.
plot(X,Y(:,1))
hold on
plot(X,Y(:,2))
plot(X,Y(:,3))
hold off
If you haven't done so already, I suggest going through MATLAB Onramp. Ch 9 covers plotting.

Products

Release

R2022b

Tags

Asked:

on 26 Aug 2023

Edited:

on 27 Aug 2023

Community Treasure Hunt

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

Start Hunting!