Clear Filters
Clear Filters

Selecting only the top half of values from a text document?

9 views (last 30 days)
I am trying to place data from a text document to a table in GUI format. However, I only need the first half of the data to be inserted into one of the columns and then the bottom half of the data to be inserted into another column. Any suggestions? Obviously the code below is incorrect. Thank you in advance.
fileID = fopen(fullfile(folder,Unifile),'r');
alldata = textscan(fileID,...
[repmat('%s',[1 14]),'%s'],'HeaderLines',10);
fclose(fileID);
nlines = length(alldata{1});
nlinestop = nlines/2
Frequency = str2num(strvcat(alldata{7}));
SingleVelocityraw = str2num(strvcat(alldata{4}));
AllVelocity = str2num(strvcat(alldata{4}));
SingleVelocity = SingleVelocityraw([1, nlinestop]);
S.tFR.Data = [Frequency SingleVelocity AllVelocity];
S.tFR_Pos = tAll_Pos;
S.tFR.ColumnEditable = [false true true true false];

Accepted Answer

Abhi Sundararaman
Abhi Sundararaman on 1 Aug 2017
Is each line of the data one entry in the table? That is, are you taking the top half of lines to be put into one column, and the bottom half into the other column? If that is the case, then you could do so by just reading the file, getting the halfway point, and indexing into the "alldata" vector.
For a simple case, with just a column of numbers in the text file that you wished to split in two, you could do this:
fileID = fopen('testfile.txt','r');
alldata = textscan(fileID, '%d');
fclose(fileID);
midline = length(alldata{1})/2;
tophalf = alldata{1}(1:midline);
bottomhalf = alldata{1}(midline:end);
This would result in the top half of the text document in one cell array, and the other half in another.

More Answers (0)

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!