How to select particular column from csv file using csvread function in Matlab

6 vues (au cours des 30 derniers jours)
POKA
POKA le 28 Nov 2017
Réponse apportée : Ronit le 4 Juin 2025
Hi Friends,
In my working place we are using Matlab student version 1999. As a work purpose I need to extract few columns of data from large csv file. So far our implementation was writing %f or %*f for all columns depending on number of columns we want.But it is cumbersome task to write so lengthy line. I have seen xlsread function to read particular column from csv file in new matlab version. But since we are using student version I am unable to get this feature. Also I have gone through csvread(filename,R1,C1,[R1,C1,R2,C2))feature of csvread but could not make success to read desired columns.
%My input is 'Navdata.csv'
Time .......missing columns not required.. Latitude(30th column) Longitude Altitude
1234 0.3567 0.4678 356
.... ..... ...... .... ...
% Using xlsread
extract_data=xlsread('Navdata.csv','Navdata','A:A')
% How can I achieve the same without reading all columns and just to extract 4 columns using csvread fucntion.
Thanks
  2 commentaires
Stephen23
Stephen23 le 28 Nov 2017
"In my working place we are using Matlab student version..."
How does a "working place" use the student version?
POKA
POKA le 28 Nov 2017
It is Matlab 5.3.1.29215a

Connectez-vous pour commenter.

Réponses (1)

Ronit
Ronit le 4 Juin 2025
Hello,
To extract specific columns from a large CSV file in MATLAB without reading all columns, you can use the textscan function.
Instead of manually specifying a long format string like "%f" and "%*f" for each column, you can dynamically generate the format string based on the total number of columns and the specific ones you want to read. The key idea is to use "%f" to read the columns you need and "%*f" to skip the rest.
The "Delimiter" option is set to match your CSV (usually a comma), and "CollectOutput" is used to gather the selected columns into one matrix. This method is efficient and works well. It's a practical and scalable approach when dealing with large datasets and avoids the overhead of loading unnecessary data.
Please refer to the documentation of "textscan" function for more details: https://www.mathworks.com/help/matlab/ref/textscan.html
I hope this helps with your query.
Thanks!

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by