Selecting only the top half of values from a text document?
2 次查看(过去 30 天)
显示 更早的评论
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];
0 个评论
采纳的回答
Abhi Sundararaman
2017-8-1
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!