Output of cell arrays into another array
1 view (last 30 days)
Show older comments
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 Comments
Stephen23
on 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.
Accepted Answer
Karim
on 17 Jun 2022
Edited: Karim
on 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)
2 Comments
Karim
on 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.
More Answers (1)
Ayush Singh
on 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
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!