populating a cell using a loop

조회 수: 1 (최근 30일)
Susan Santiago
Susan Santiago 2018년 10월 7일
댓글: Susan Santiago 2018년 10월 8일
This is my code
a = dir;
i=3;
n=1;
b = zeros(n,55);
while i<=43
d = a(i).name;
A = textscan(fopen(d),'%f %f %f %f %f %f %f %f %f %f %f %f %f %f %s %s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %s %s %f %f %f %f %f %f %f %f', 'Delimiter',',','Headerlines',1);
A_time = A{1,1};
B_time = num2str(A_time);
x = datenum(B_time,'yyyymmddHHMM');
b = A(n,:);
i=i+1;
n=n+1;
end
I've for help with this code in a couple different ways and while I appreciate any help, a lot of it has not been what I'm looking for. I'm trying to run this but when I do, it says that the index exceeds matrix dimensions for the line b=A(n,:). Please just help me figure out why this isn't working. I know it's difficult without the data files but just know that they all have 55 columns. When the part in the loop is run by itself up to b, A is a 1x55 cell. I want b to be a cell with 55 columns but with n rows. one row for each data file that I run. Please just tell me what is wrong with this code. Let me know if clarification is needed
  댓글 수: 2
Stephen23
Stephen23 2018년 10월 7일
편집: Stephen23 2018년 10월 7일
"populating a cell using a loop"
A is already a cell array, and it contains all of the imported data. So what advantage do you see in copying all of that data into a new cell array?
Note that it would be much more efficient to import the file once before the loop, rather than importing the same file 43 times just to extract one row each time.
Susan Santiago
Susan Santiago 2018년 10월 7일
A only has imported data from 1 file. I would like to have an array with the information from all iterations together.

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

채택된 답변

Stephen23
Stephen23 2018년 10월 7일
편집: Stephen23 2018년 10월 7일
textscan returns a 1x55 cell array, where the format string specifies the 55. So your code:
A = textscan(fopen(d),'%f %f %f %f %f %f %f %f %f %f %f %f %f %f %s %s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %s %s %f %f %f %f %f %f %f %f', 'Delimiter',',','Headerlines',1);
will result in a 1x55 cell array. Then a few lines later you write this:
b = A(n,:);
because A only has one row, as soon as n>1 this will be an error, because C only has one row, and so requesting anything from its (non-existent) second (or higher) row will be an error.
"I want b to be a cell with 55 columns but with n rows. one row for each data file that I run"
I really really really recommend that you don't do that: that would require putting scalar numeric data into the cells of a cell array, which just makes it much harder to work with numeric data. You should really keep the numeric data in numeric arrays, or use a table. Because you have a few columns which are character, this complicates the importing a little bit, but there are reasonable solutions which you should look at:
  • If you do not need those character columns then get textscan to ignore them with %*s in the format string. Then could trivially get textscan to collect all of the numeric data into one numeric array, using the CollectOutput option. Very simple, but you would lose some data.
  • Use the CollectOutput option to collect the data into arrays of matching types: this would give you one 1x5 cell array C, containing an Nx14 numeric array, an Nx2 char cell array, an Nx29 numeric array, an Nx2 char cell array, and an Nx8 numeric array (or whatever sizes that format string gives you). I recommend this option.
  • Use a table. These are a very convenient way for handling mixed data (e.g, numeric, char, categorical) and analyzing it. It has many powerful methods and operators for processing data by groups, and for statistical analyses.
Or your proposal:
  • If you really want to get all of your data into one cell array (which will make any numeric processing slow, inefficient and complex), then you will need to post-process the data after it has been imported, something like this: detect numeric columns, convert numeric columns to cell array containing numeric scalars (e.g. num2cell), then concatenate all into one cell array. I strongly advise you to avoid doing this.
  댓글 수: 13
Susan Santiago
Susan Santiago 2018년 10월 8일
I think at least part of the problem is that I incorrectly thought that some of the columns were fine as %f when they weren't because I just went back and part of a very similar code that worked fine before was only outputting one row of data after making that change. So the four columns that are labelled as strings, I think need to stay that way
Susan Santiago
Susan Santiago 2018년 10월 8일
I was able to fix the issue! Thank you so much for your help and patience, I learned a lot today

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by