0x1000001
0x1000001

Reputation: 137

MATLAB - Plotting matrices of different size on same plot - error in plot function

I am importing data from three files and parsing it to obtain time and voltage values from each file. These values need to be plotted against each other on the same plot.

The data is held in a total of six matrices, one for time and one for voltage for each of the three data sets.

Matrix dimensions: matlab data sets: 1000x1, ltspice: 465x1, oscope: 2500x1.

Matlab finds an error in the use of the plot function:

plot(matlab_t,matlab_v,'k',ltspice_t,ltspice_v,'j',oscope_t,oscope_v,'g');

Is this an issue because the matrix dimension vary between independent and dependent sets?

Full code for script:

clear;
clc;

%% Import
%Read in files

matlab_t=dlmread('ENGR_222_Project_1_data.csv',',',[16 0 1015 0]);
matlab_v=dlmread('ENGR_222_Project_1_data.csv',',',[16 1 1015 1]); 

ltspice_t=xlsread('ltspicedata_excel.xlsx','A1:A465');
ltspice_v=xlsread('ltspicedata_excel.xlsx','B1:B465');

oscope_t=xlsread('oscope_data.xlsx','D1:D2500');
oscope_v=xlsread('oscope_data.xlsx','E1:E2500');

%% Plot

plot(matlab_t,matlab_v,'k',ltspice_t,ltspice_v,'j',oscope_t,oscope_v,'g');

Upvotes: 0

Views: 863

Answers (1)

0x1000001
0x1000001

Reputation: 137

To plot multiple matrices on the same plot, each matrix must have the same dimensions. In the case where we have two 465 X 1 matrices, two 1000 X 1 matrices, and two 2500 X 1 matrices, all matrices must have the dimension 2500 X 1.

To increase the dimensions of the of the smaller matrices, redefine the matrix to that size and set the empty cells equal to zero.

This is accomplished in the following code:

matlab_t(2500,1)=0;
matlab_v(2500,1)=0;
ltspice_t(2500,1)=0;
ltspice_v(2500,1)=0;

Complete code using fix:

clear;
clc;

%% Import
%Read in files

matlab_t=dlmread('ENGR_222_Project_1_data.csv',',',[16 0 1015 0]);
matlab_v=dlmread('ENGR_222_Project_1_data.csv',',',[16 1 1015 1]); 

ltspice_t=xlsread('ltspicedata_excel.xlsx','A1:A465');
ltspice_v=xlsread('ltspicedata_excel.xlsx','B1:B465');

oscope_t=xlsread('oscope_data.xlsx','D1:D2500');
oscope_v=xlsread('oscope_data.xlsx','E1:E2500');

% Redefine matrices to equal size

 matlab_t(2500,1)=0;
 matlab_v(2500,1)=0;
 ltspice_t(2500,1)=0;
 ltspice_v(2500,1)=0;

 %% Plot

plot(matlab_t,matlab_v,'k',ltspice_t,ltspice_v,'j',oscope_t,oscope_v,'g');

Upvotes: 1

Related Questions