MATLAB: Variable definition

csvcsvreaddata importindexingMATLABreshapesignal processing

It is assumed there are 20000 values in a row.(as csv file)
I use "load" command, I can define new variable include all data. But I want to define variables that using partially data.
For example, data from No.1 to No.100 as variable "val_1", and data from No.101 to 200 as variable "val_2", …..and data from No.19901 to No.20000 as "val_200".
Can you tell me how to write the MATLAB code? Thank you.

Best Answer

  1. Instead of having 200 separate variables, it might be easier to create a single variable containing the 20,000 values arranged as a 2D matrix, consisting of 100 rows x 200 columns (or vice versa). This arrangement of your data will make it much easier to analyze and visualize the data in MATLAB, which is inherently based on vectors and matrices.
  2. Also, you may want to consider using the csvread function instead of the load function to read the data in from your CSV file.
Please try the following:
filename = 'myfile.csv';
X = csvread(filename);
Y = reshape(X,100,200);
Now you can access any one of the 200 columns of data as follows:
k = 40; % select a particular column k
V = Y(:,k); % all rows for the k-th column
HTH.
Best, Rick