Efficient way to assign single vectors to a cell array

조회 수: 5 (최근 30일)
Pietro Murialdo
Pietro Murialdo 2019년 10월 20일
댓글: Pietro Murialdo 2019년 10월 20일
Hello everybody,
I have M vectors, each one containing N(i) elements [with i = 1:M]; so the length of each vector is variable. I read each vector with a for loop. What I would like to do is creating a cell array, with M cells, where each cell (i) contains NOT a "reference" to the vector, but the N(i) elements of each vector.
This is what I am doing,
monthName = {'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', ...
'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'};
for j = 1:length(monthName)
filename = [directory, 'series', monthName{j}, '.mat'];
tmp = load(filename);
whos tmp
rawSeries{j}(:,1) = tmp;
end
And I get (I reported only 2 of 12 outputs),
Name Size Bytes Class Attributes
tmp 1x1 4046744 struct
Name Size Bytes Class Attributes
tmp 1x1 3653128 struct
And the cell array rawSeries is a 1x12 cell, where each cell is a 1x1 struct that contains the N(i)x1 vector. This is not what I want.
I would like rawSeries to be a 1x12 cell array, where each cell contains N(i)x1 vector. So that,
rawSeries{1}(:,1)
rawSeries{2}(:,1)
would print the whole vectors cantained in the first and second cell.
Then if it is possible, I would like to do that in the fastest way since I have very large datasets.
Thank you in advance

채택된 답변

Matt J
Matt J 2019년 10월 20일
편집: Matt J 2019년 10월 20일
for j = 1:length(monthName)
filename = [directory, 'series', monthName{j}, '.mat'];
S = load(filename);
rawSeries{j}(:,1) = S.whatever; %"whatever" should be the name of the variable in the .mat file
end
  댓글 수: 1
Pietro Murialdo
Pietro Murialdo 2019년 10월 20일
Ok thank you very much, I was missing the .whatever and I could not understand

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by