How do i remove all blank lines from excel sheet in matlab?

9 views (last 30 days)
I have an excel sheet with datas in it.I want to print only those rows which has particular words matching in it in a seperate variable. When i print the data in the temp variable i get the datas printed which i require, but also blank rows.
I will attach my code and a screen shot of the output i am getting. Kindly help me with this.
[file,path] = uigetfile({'*.xls;*.xlsx','Excel Files'},' Select data file');
filename = strcat(path,file);
[num,txt,raw] = xlsread(filename,'Profiles');
[rows,columns] =size(raw);
%So i iterate through the entire excel sheet by i,j.Have to go through only
%i,1 and see whether the cell inside has the zord inside cell match .if one
%word matches,in another temporary raw file,i put tempraw =raw(i,:)
%so now i get th fitered excel sheet
for i =2:rows
cellMatch={'distance','height','markingOffset','circulationWay','name','type'};
matchStr =strsplit(raw{i,1},'.');
matchStr_datasize =length(matchStr);
for j=1:length(cellMatch)
for k = 1:matchStr_datasize
if strcmp(matchStr{k},cellMatch{j})
tempRaw(i-1,:) = raw(i,:);
end
end
end
end
The images are consecutive images

Answers (2)

jonas
jonas on 20 May 2018
Edited: jonas on 20 May 2018
One fix would be to remove the empty lines, which is quite simple. However, a better fix would be to not introduce this issue to begin with, for example by changing the way you specify the row in your inner loop. Instead of i (which increases by 1 even when your statement is false), use a variable that increases by 1 for each entry. Just add n=0 before the loop and then:
n=n+1
tempRaw(n,:) = raw(i,:);
However, I would prefer to skip the inner loop altogether. This line will save all lines of A where the column col matches a string str, without empty lines.
B=A(strcmp(A(:,col),str),:)

Image Analyst
Image Analyst on 20 May 2018
"The images are consecutive images" -- Huh??? What images?
One way would be to edit the workbook(s) in place using ActiveX. How familiar are you with ActiveX? If not, do you want to learn?
The other option would be to read in the workbook into MATLAB, then remove blank rows, then write the new workbook out with the same name. You need to delete the workbook because xlswrite() just blasts over existing data and if you didn't do this you'd have old rows below the new row because the old block of cells was taller than the new block of cells.

Tags

Community Treasure Hunt

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

Start Hunting!