我有一个包含消息和时间戳的文本文件:
{193220,message,ucontroller9,controller1,lowalarm,success},
{193220,message,controller1,changer,{change,down},success},
{193220,controlaction,tapchange_down,{oldsetting,0.975},{newsetting,0.95}},
{193220,modelupdate_start,changeup,changer,193220},
{14430,modelupdate_complete},
{278480,message,ucontroller6,controller1,highalarm,success},
{278480,message,controller1,changer,{change,up},success},
...
我想将这些行读入一个数组并根据第一列重新排序数组。到目前为止,我的方法如下:
fid = fopen('messagestore.txt') % Open file
n=1
while 1
string = fgetl(fid); % get line
if ~ischar(string), break, end % break if end of file
string = strrep(string,'[',''); %
string = strrep(string,']',''); %
string = strrep(string,'{',''); % strip out unwanted characters
string = strrep(string,'}',''); %
string = strrep(string,',',' '); %
string = string(2:end); % remove space at start
MessageArray(:,n) = textscan(string,'%f%s%s%s%s%s'); % format and save into array
n=n+1;
end
fclose(fid)
这部分有效,但我在区分不同的消息类型(即字段数)时遇到了问题。我猜sort
如果我可以正确地将数据放入数组中,我可以使用 matlab 函数。
有没有办法在matlab中做到这一点?