필터 지우기
필터 지우기

how to read spread sheet row by row iteratively ?

조회 수: 1 (최근 30일)
Abdulaziz Abutunis
Abdulaziz Abutunis 2015년 11월 8일
답변: Walter Roberson 2015년 11월 8일
Hello everyone,
I have an excel sheet that contains several rows with different lengths. I want to read these rows, using the command xlsread, iteratively ( in each iteration the code read one row) that is to avoid reading all the spreadsheet once which will generate matrix with NaN elements that will affect my calculation. I will set variable to hold the row and clear this variable after each calculation.
Thanks

채택된 답변

Walter Roberson
Walter Roberson 2015년 11월 8일
Yes, it is possible to specify a Range that is a single row. You would want to use xlsread1 from the File Exchange to improve the speed -- though I understand that in R2015b they do something similar natively.
But there is another way:
num = xlsread('TheFileName.xls');
num_by_line = mat2cell(num, ones(1,size(num,1)), size(num,2));
trimmed_lines = cellfun(@(C) C(~fliplr(cumprod(fliplr(isnan(C))))), num_by_line, 'Uniform', 0);
Now trimmed_lines is a cell array of numeric vectors, one entry per row, in which all the trailing NaN have been removed.
A question would be whether you want the same thing to happen to leading NaN.
Note: NaN also occur in the numeric form in places where strings occur. If you have strings that you want to preserve then the code gets more complex. Leading NaN can occur as row headers.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by