Filter löschen
Filter löschen

Creating a cell array dynamically & manipulating matrices inside the cell array

25 Ansichten (letzte 30 Tage)
Hi,
I am trying to create a cell array dynamically.
I am reading several matrices off of an excel file. For example: If I read 4 matrices: A,B,C,D.
I will create cell array E={A,B,C,D} but, I want the code to create this cell array dynamically depending on how many excel files (or sheets) I read.
Basically, it should be able to figure out the number of matrices and assign it variables in the cell array without me having to tell it how many matrices I have read from the excel sheet.
For example: If I read 8 excel sheets I should get 8 matrices in the cell array & If I read 12 matrices I should get 12 matrices in the cell array,etc. dynamically.
I am also wondering about how to manipulate each matrix inside the cell array like I normally would do without the cell array.
I know how to select a specific matrix inside the cell array but, I don't know how to manipulate a specific matrix inside the cell array & store it somewhere else. Can this be only done with the help of a 3D matrix or is there another way?
There is a reason I am using the cell array so, I don't think that I will change that part but, maybe If I can do the same thing dynamically using a 3D matrix instead of a cell array in an easier way I can accept that too.
The reason is I am using a for loop which does the same calculation over and over again but, changes the matrix inside the calculation & I am supposed to manipulate the elements inside each matrix & use multiple matrices at the same time for the calculation (to be specific 2 matrices at the same time & manipulate these matrices).
I can write the code the normal way & it will do the job but, the number of lines would be a lot to do the same exact thing.
I am not sure if this is detailed enough so, if you need more details I can show you why I am trying to use a cell array but, I just felt like it will be a longer explanation & that's why I tried to cut it short here.
Any help is appreciated. Thank you in advance.
  1 Kommentar
Stephen23
Stephen23 am 14 Dez. 2017
Bearbeitet: Stephen23 am 14 Dez. 2017
"I want the code to create this cell array dynamically depending on how many excel files (or sheets) I read."
Easy: create a cell array of the required size. Add the matrices directly using indexing (like Jos showed you).
"I am also wondering about how to manipulate each matrix inside the cell array like I normally would do without the cell array"
Easy: use indexing in a loop, or cellfun (like Jos showed you).
Stop thinking of the matrices being individually names variables: this will only make things confusing for you. Just follow what Jos showed you.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Jos (10584)
Jos (10584) am 13 Dez. 2017
I am not sure if I understand you correctly. At some point you specify how you will read in your data and from where. This should also give you N
N = ... % number of matrices/files to read
E = cell(1,N) ;
for k=1:N
Matrix = MyReadFunction(...) % ??
E{k} = Matrix ; % dynamically create E
end
To apply the same function on all cells in E you can use cellfun.
function Y = MyFunction(X)
Y = fliplr(X) % silly example
Which you can use like this
E2 = cellfun(MyFunction, E, 'un',0)
  1 Kommentar
Sumedh Kulkarni
Sumedh Kulkarni am 24 Jan. 2018
Jos, Thanks! I followed your method and got a cell with dynamic double arrays in it. However, I want to add another array (from different matrix) in each of the dynamic double arrays . Is it possible to do so? Thanks

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by