How to import a column from an Excel file?

조회 수: 13 (최근 30일)
Giuseppe
Giuseppe 2021년 4월 16일
답변: Giuseppe 2021년 4월 17일
This is my excel file:
Here I read the entire column A of the Excel sheet named "Lazy_eight" but the problem is that I have different sheets in which the column A has a different number of elements. So, I want to import only the numbers without specifing the length of the column vector.
I use the function readmatrix with the following syntax in order to read the entire column:
p_time = readmatrix('Input_signals.xlsx','Sheet','Lazy_eight','Range','A:A')
i get this in matlab workspace:
So, I wish to give to the "readmatrix" function only the first element of the column I want to import but I want that it stops at the last element, without specifing the coordinate of the last element in order to avoid the NaN that you can see in the last image. I want to import only the numbers without the NaN value.
I cannot read the initial and the last element (in this way: 'Range', 'A3: A13') beacuse in every sheet the column A (as the other ones) has a different number of elements.
  댓글 수: 2
João Mendes
João Mendes 2021년 4월 16일
Maybe you can just import the entire matrix and then, in matlab, isolate the first column? Or that is not what you want?
Giuseppe
Giuseppe 2021년 4월 16일
@João Mendes, thanks for your answer but I'd prefer to avoid to use other variables in my code.

댓글을 달려면 로그인하십시오.

채택된 답변

Clayton Gotberg
Clayton Gotberg 2021년 4월 16일
Since it seems that you just need to get rid of the NaN values, try using logical indexing with the isnan command.
p_time = p_time(~isnan(p_time));
This will remove all NaN values from the p_time vector.
As an example:
p_time = [5 NaN 3 7 NaN]; %double
eliminate_NaN_logical = ~isnan(p_time)
returns
eliminate_NaN_logical = [1 0 1 1 0]; %logical
and
p_time_cropped = p_time(eliminate_NaN_logical)
returns
p_time_cropped = [5 3 7]; %double
because MATLAB assumes you want a vector with only the elements for which eliminate_NaN_logical is true.

추가 답변 (1개)

Giuseppe
Giuseppe 2021년 4월 17일
Another more rapid solution is to use “rmmissing” function to remove “NaN” values from an array.

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by