Output of cell arrays into another array

조회 수: 3 (최근 30일)
Lieke Pullen
Lieke Pullen 2022년 6월 17일
편집: Karim 2022년 6월 17일
I've extracted information from 142 different files, which is stored in CSV-file with two columns, which contains both number and text. I want to copy row 11-145 of the second column, and paste it into a matrix/cell. Then, I want to skip the next 10 rows, and copy row 156-290 and paste it into the next column etc etc. I have tried the following code:
original=readcell('sample_file.csv');
overview=cell(145,135);
for i=1
for j=1
overview{:,i}=original{i+10:i+144,2};
j=j+1;
end
i=i+1;
if i>20590 % this is the total number of rows in the original file
break
end
end
However, this gives me the following error: Assigning to 145 elements using a simple assignment statement is not supported. Consider using comma-separated list assignment.
Can somebody help me out please?
  댓글 수: 4
Stephen23
Stephen23 2022년 6월 17일
@Lieke Pullen: a sample data file does not have to contain confidential data. It just has to have the same format and accurately represent the delimiters, end-of-lines, and values (e.g. numeric formats) found in the actual data files.
Generally descriptions of data tend to be much less accurate than sample data itself.
Lieke Pullen
Lieke Pullen 2022년 6월 17일
@Stephen23 You are right, I have created a small sample file and attached it to the code.

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

채택된 답변

Karim
Karim 2022년 6월 17일
편집: Karim 2022년 6월 17일
Hello,
Here you have a bit of example code with some random data, hope it helps.
numFiles = 142;
TotalRows = 20590;
SkipRows = 10;
% create some pseduo data
SomeData = {'String 1' 1 'String 2' 2 'String 3' 3};
% assume we want some cell with at least TotalRows rows and 2 columns
original = SomeData(randi(numel(SomeData),TotalRows,2));
% generate the indices of the data we want to extract
SkipMe = 1:(TotalRows/numFiles):TotalRows;
SkipMe = repmat(SkipMe,SkipRows,1) + repmat((0:(SkipRows-1))',1,numel(SkipMe));
SkipMe = SkipMe(:);
CopyMe = true(TotalRows,1);
CopyMe(SkipMe) = false;
% now extract the data from the 2 column
overview = original(CopyMe,2);
% reshape the data so that each column represents 1 file
overview = reshape(overview,[],numFiles);
size(overview)
ans = 1×2
135 142
  댓글 수: 2
Lieke Pullen
Lieke Pullen 2022년 6월 17일
Hi KASR, thank you for your answer, your script works! However, now the 136th row in first column is already the first row of what supposed to be the second column. Thus, they are shifted and the dimensions of the matrix actually have to be 135x142 (that is my mistake, my apologies). How do I adjust that?
Karim
Karim 2022년 6월 17일
ah yes, this is because i switched the indexes on the last row it should state:
overview = reshape(overview,[],numFiles);
to have the files as columns.
I will modify the example code above.

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

추가 답변 (1개)

Ayush Singh
Ayush Singh 2022년 6월 17일
Hi Lieke,
From your question I understand you want to extract some portion of data from your csv file.
You could use readtable function to read your file in table format and then apply the matrix operations on the output table
To extract in table format -
table = readtable('document.csv')
And suppose to extract certain row and column then
extracted_table = table(11:145,:2) % 11-145 row of 2nd column

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by