How do I shorten and remove empty elements from a textscan cell array?
3 views (last 30 days)
Show older comments
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
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
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.
Answers (1)
Stephen23
on 22 Jul 2015
Edited: Stephen23
on 22 Jul 2015
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
See Also
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!