filter certain column in an array
Show older comments
I have data array as following, the first 2 lines are not shown in the csv file.
----Student 1---- ----Student 2---- ----Student 3---- ----Student 4---- ----Student 5---- %not shown in csv%
Eng. Math Science Eng. Math Science Eng. Math Science Eng. Math Science Eng. Math Science %not shown in csv%
50 20 100 80 80 60 80 50 60 90 100 20 50 100 100
80 30 60 50 60 70 80 30 70 50 0 90 70 100 90
60 70 80 30 90 40 100 90 60 10 80 40 80 40 60
I have created a table to allow the user to filter some students out of this table. For example, if student 4 is selected. StudentKept = [1 2 3 5]. Then may I know how to re-construct the csv file with only StudentKept??
Current coding:
StudentKept=find([data{:,2}]==0);
for m = studentKept
dataArrayFiltered=[dataArrayAll(studentKept,1)];
end
2 Comments
Sivakumaran Chandrasekaran
on 12 Jan 2016
have you checked the option csvwrite.
Adithya Addanki
on 14 Jan 2016
Hi,
There is no direct way of doing this through "csvwrite" or "xlswrite". I think you will be able to achieve it in a programmatic way.
You may need to extract the columns that are not being selected(may be a flag to see if it is selected or not? or indexing actually through all the data and selecting the unfiltered data based on row and column indexes).
Thank you,
Adithya
Answers (1)
Guillaume
on 14 Jan 2016
The first thing to do is to get rid of the flat format of your file, which is not helpful for manipulating data. Reshaping the data so that students are the third dimension makes it easy to filter by student:
studentkept = [1 2 3 5];
studentdata = csvread('somefile.txt');
studentdata = reshape(studentdata, size(studentdata, 1), 3, []); %pages no correspond to student
keptdata = studentdata(:, :, studentkept); %now, it's easy to filter
You can save it back to the original format by reshaping back to the flat format:
csvwrite('someotherfile.txt', reshape(keptdata, size(keptdata, 1), []));
Categories
Find more on MATLAB 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!