Output of cell arrays into another array
1 次查看(过去 30 天)
显示 更早的评论
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
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.
采纳的回答
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)
2 个评论
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
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!