MATLAB Answers

Creating a Cell to save n*n matrices

9 views (last 30 days)
serena solanki
serena solanki on 27 Mar 2018
Commented: serena solanki on 27 Mar 2018
Hi All Basically I have a structure which I am carrying out a sensitivity analysis on. I have 7 accelerogram data sets which I am looping through. I am also changing the stiffness and damping parameters for the dampers used and I am calculating the peak displacements.
So I have saved the peak displacements as an n*n cell for the corresponding stiffness and damping values. Is there a way to save this matrix within another matrix or cell so that I have 7 sets of these matricies? One set for each Record which I am using?
I have tried 3D matrices but it is not quite what I need- unless I have implemented it wrong. I am also struggling with where to place the cell so it saves the data correctly.
Hope you can help and Thanks in advance!
str='E:\01 LOUGHBOROUGH UNI WORK\2017-2018 4th Year\FINAL YEAR\DISSERTATION\SERENA SOLANKI DISS\CODING\california earthquake record';
textfiles=dir([str '*.txt']);
for num=1:1:7
textfilename=['Record_', num2str(num), '.txt']; %read Record_ numbers 1-7
%open textfile 'rt' is for reading a text file only
ft{num}=m*acceleration_data.'; %force data is saved in a cell
kiso_range=0.05:0.05:0.9; %changing the base isolator stiffness of the structure
for index=1:iso_range
for cc=1:ziso_r
M=[m 0;0 m];
K= [k -2*kiso
-2*kiso 2*kiso+k ];
tau=[1 1 1 1 1 1]';
b=[0;(-1)]; %force vector
A=[a b, c d];
dt = 0.02; %seconds
NPTS = size(ft{num},2);
tf = (NPTS-1)*dt;
t = 0:dt:tf;
dt_interp = 0.005;
t_interp1{num} = dt_interp:dt_interp:tf;
ft_interp = F_interp(t, ft{num}, t_interp);
NPTS_interp = size(t_interp,2);
yt = zeros(12,NPTS_interp);
for i=1:(NPTS_interp-1) % Runge-Kutta loop, i = incremental time step
g1 = A*yt(:,i)+b*ft_interp(i);
g2 = A*(yt(:,i)+ 0.5*g1*dt_interp)+0.5*b*(ft_interp(i)+ft_interp(i+1));
g3 = A*(yt(:,i)+ 0.5*g2*dt_interp)+0.5*b*(ft_interp(i)+ft_interp(i+1));
g4 = A*(yt(:,i)+g3*dt_interp)+b*ft_interp(i+1);
ynew = yt(:,i)+(g1+2*g2+2*g3+g4)*dt_interp/6;
yt(:,i+1)=ynew; %storage
% yt= displacement, the indicates the floor number
Peak_disp{num,index,cc}=max(abs(yt(2,:)-yt(1,:))*0.01); % saving peak displacements for each data set


Bob Nbob
Bob Nbob on 27 Mar 2018

Since I didn't write the code I'm going to need a bit of help with your variable names. If I am interpreting what you're asking correctly then you want a 1x7 (or 7x1) cell array where the cells contain the nxn matrices of values you calculated in the inner loop?

To the best of my knowledge cells can contain any class of items, so you should be able to just define the cell contents as the matrix using indexing.

Peak_disp(num) = {Peak_mat}; % Parentheses around num indicates that we are
                             % defining the entirety of num element. Curly
                             % braces around Peak_mat defines the num element
                             % as a cell containing the Peak_mat matrix.

You shouldn't need to use 'index' or 'cc' in your indexing of Peak_disp, as these values are a fixed value from loops that have already ended.

On a side note, you can loop 'num' using num = 1:7. MATLAB defaults to increasing by 1 when no specific interval is defined.

serena solanki
serena solanki on 27 Mar 2018

Hi Bob

Thanks for the tip, after a little trial and error I found that


gave me the answer I needed.

I wanted to create a 1*7 cell which contained the n*n matrices which refer to cc and index- which are for the values calculated in the inner loop.

Again thanks for the help

Sign in to comment.

Answers (0)

Sign in to answer this question.