How do I shorten and remove empty elements from a textscan cell array?

3 views (last 30 days)
Here's my code
FILE_NAME='17.5x5burnerAug28_2014y25CEA180scool.txt';
file=fopen(FILE_NAME);
file_strings=textscan(file,'%s','delimiter','\t');
fclose(file);
help = 0;
while help < 138488
help = help +1;
if strcmp(file_strings{1}{help}, '')
file_strings{1}(help) = [];
disp(help);
end
end
When I run this code, all of the empty string elements in the cell array remain in the same index, and the size of the cell array remains the same. I want to be able to delete each empty element in the cell array.
  2 Comments
Stephen23
Stephen23 on 22 Jul 2015
This time I formatted your code correctly for you, but in future please format it yourself by selecting the code and then clicking the {} Code button that you will find above the textbox.
Star Strider
Star Strider on 22 Jul 2015
What information do you want from your text file?
It has several lines of header information, then a large number of different matrices with essentially the same header and numerical format, apparently from different experiments.

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 22 Jul 2015
Edited: Stephen23 on 22 Jul 2015
Instead of a loop you can use cellfun to detect the empty strings:
file_name = '17.5x5burnerAug28_2014y25CEA180scool.txt';
fid = fopen(file_name,'rt');
C = textscan(file,'%s','delimiter','\t');
fclose(file);
out = C{1}(~cellfun('isempty',C{1}(:,1)),:);
You do not actually give any sample data and do not tell us how many rows and columns this data has, so I tried to make it work for multiple columns, but it is untested as I do not have your data.
  2 Comments
Richard  Lin
Richard Lin on 22 Jul 2015
Here's my text file. Also what function would I use in order to delete all of the empty cells and then condense the cell array? Thank you for you initial answer by the way.
Stephen23
Stephen23 on 26 Jul 2015
Edited: Stephen23 on 26 Jul 2015
That is quite a complicated text file. A Star Strider has already asked you, exactly what information do you want to retrieve from this text file? If you tell us exactly what you are after then we can figure out an efficient way of doing it.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!