Output of cell arrays into another array

1 Ansicht (letzte 30 Tage)
Lieke Pullen
Lieke Pullen am 17 Jun. 2022
Bearbeitet: Karim am 17 Jun. 2022
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 Kommentare
Stephen23
Stephen23 am 17 Jun. 2022
@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 am 17 Jun. 2022
@Stephen23 You are right, I have created a small sample file and attached it to the code.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Karim
Karim am 17 Jun. 2022
Bearbeitet: Karim am 17 Jun. 2022
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 Kommentare
Lieke Pullen
Lieke Pullen am 17 Jun. 2022
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 am 17 Jun. 2022
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Ayush Singh
Ayush Singh am 17 Jun. 2022
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

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by