Output of cell arrays into another array

4 visualizzazioni (ultimi 30 giorni)
Lieke Pullen
Lieke Pullen il 17 Giu 2022
Modificato: Karim il 17 Giu 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 Commenti
Stephen23
Stephen23 il 17 Giu 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 il 17 Giu 2022
@Stephen23 You are right, I have created a small sample file and attached it to the code.

Accedi per commentare.

Risposta accettata

Karim
Karim il 17 Giu 2022
Modificato: Karim il 17 Giu 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 Commenti
Lieke Pullen
Lieke Pullen il 17 Giu 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 il 17 Giu 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.

Accedi per commentare.

Più risposte (1)

Ayush Singh
Ayush Singh il 17 Giu 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

Categorie

Scopri di più su Cell Arrays in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by