MATLAB Answers

what is the easiest way to reduce the lines in the code below?

1 view (last 30 days)
muhammad choudhry
muhammad choudhry on 24 Nov 2020
Edited: dpb on 24 Nov 2020
I need to read every row but certain column in the csv file. Does anyone know how to modify the code below?
File = csvread('flowrate.csv');
1 = File(:,1)
2 = File(:,2)
3 = File(:,3)
4 = File(:,4)
5 = File(:,5)
7 = File(:,7)
8 = File(:,8)
9 = File(:,9)
10 = File(:,10)
11 = File(:,11)
Thanks in advance

  0 Comments

Sign in to comment.

Accepted Answer

dpb
dpb on 24 Nov 2020
data=csvread('flowrate.csv');
% Solution 1: Eliminate unwanted column
data(:,6)=[];
% Solution 2: Keep desired columns
data=data(:,[1:5 7:11]);
Spend some time reading and working examples <Matrices and Arrays> to learn basic MATLAB syntax.

  3 Comments

muhammad choudhry
muhammad choudhry on 24 Nov 2020
hi,
Thanks, can you explain a little bit. how to remember stuff like this as I am keep reading the documents but seems like I am getting stuck everytime I do something new.
data(:,6)=[]; can you explain this line please! and thanks for the modified code.
dpb
dpb on 24 Nov 2020
I don't what to tell you other than to read the explanations and see the general principles behind the examples. The <Array indexing> link at the bottom of the above page starts off with "there are three primary approaches to accessing array elements based on their location (index) in the array. These approaches are indexing by position, linear indexing, and logical indexing."
This is followed by a section on each of the three techniques where in the first section is the information that for multiple elements you can "reference multiple elements at a time by specifying their indices in a vector." and the example of that as r = A(2,[1 3])
It then continues directly thereafter with "To access elements in a range of rows or columns, use the colon." and an example for that syntax as well, r = A(1:3,2:4).
Those are some of the basic principles of MATLAB syntax; they apply to any general indexing situation where you know the numeric value of the desired indices.
As far as the [], see the link <Removing Rows or Columns from a Matrix> also at the bottom of the page to which I pointed you before.
As said, you simply must spend some time reading the doc and assimilating the general rules that are explained; there's no substitute for that effort.
There's an OnRamp training course many apparently find helpful; I don't have the direct link to it; I'm certain a search for the term should find it for you.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!