How to plot a matrix within a matrix?
14 views (last 30 days)
Show older comments
Herline van der Spuy on 20 Jun 2021
Commented: Joel Lynch on 21 Jun 2021
I have 5 temperatures and for each temperature a stiffness and deflectionmeasurement was taken at 8s, 15s, 30s, 60s, 120s, 240s. But the data I've received is all on one matrix, like the -30° is directly after the -36°C. (I've got 5 temperatures). So column one is basically the time one after the other and the 2nd column is the stiffnesses and 3rd the deflection. I know the order. It is always increasing (-36°C to -30°C to -24°C, etc.) But I don't know how to split them into 5 different matrices and plot them each, but not the ones with zeros. I have tried creating an array without the zero values, meaning I remove those rows, but I still struggle.
Joel Lynch on 20 Jun 2021
Edited: Joel Lynch on 20 Jun 2021
In terms of organizing the data, the simplest approach is to create two matricies for stiffness and deflection that have two dimensions, time and temperature. Assuming these vectors are constant and known in advance, they can be defined as:
time_series(:,1) = [8,15,30,60,120,420]; % along dimension 1
temp_series(1,:) = -36:6:-18; % along dimension 2
Note: It is generally good practice to have the dimensions of these vectors correspond to the matrix you construct. Here, time is chosen to be a column vector, defined along dimension 1, and temperature is a row vector, defined along dimension 2.
It is also useful to have the length or number of elements along each dimension:
Ntime = numel( time_series );
Ntemp = numel( temp_series );
Next, assuming your spreadsheet is loaded into a matrix M, we can construct stiffness and deflection matricies that match these dimensions. This is done by reshaping the 2D matrix M:
stiffness(1:Ntime,1:Ntemp) = reshape( M(:,2) , [Ntime, Ntemp] );
% Read as: All rows in col. 2 of M are reshaped to matrix "stiffness" of size Ntime x NTemp
deflection(1:Ntime,1:Ntemp) = reshape( M(:,3) , [Ntime, Ntemp] );
% Read as: All rows in col. 3 of M are reshaped to matrix "deflection" of size Ntime x NTemp
Then, if you want to plot time-series of stiffness and deflection for each temperature, excluding the first temperature:
subplot(2,1,1); hold on; grid on; title('Stiffness over Time');
plot( time_series, stiffness(:,2:end), '-x')
subplot(2,1,2); hold on; grid on; title('Deflection over Time'); xlabel('Time (sec)')
plot( time_series, deflection(:,2:end), '-x')
% optional legend
legend( sprintfc('%d',temp_series(2:end)) )
This assumes the first temperature is always zero, another approach is needed if the location of zeros is at some other time or temperature. I get the following figure:
Joel Lynch on 21 Jun 2021
Subplot(A,B,N) creates or selects the current axis that subsequent plot commands appear on. Calling “Hold on” tells matlab not to overwrite exist plot commands in that axis. So plotting additional data is as simple as issuing a similar plot command.
FYI, the first two arguments of subplot determine the topography of the subplots (i.e. the number of rows and columns of subplots) and the last selects the linear index of the specific axis within that range.
More Answers (0)
Find more on Shifting and Sorting Matrices 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!