簡體   English   中英

有什么方法可以加速必須在20000+行中搜索16x字符串的Matlab代碼嗎?

[英]Any way to speed up Matlab code that has to search through 20000+ rows for a string 16x?

基本上,代碼的工作方式是用戶鍵入一個字符串(在我的情況下為7:29:29 AM-2:33:33 PM這樣的時間戳),然后代碼從excel中讀取包含這些字符串的數據文件。它和所有數據:filename = get(handles.File,'String'); [數據,文本] = xlsread(filename,2);

IndexStart=strmatch(get(handles.StartTime,'String'),Text,'exact'); %start time
IndexEnd=strmatch(get(handles.EndTime,'String'),Text,'exact'); %end time

seconds = IndexEnd-IndexStart;
PlotData = Data([IndexStart: IndexEnd],:);

然后,它在“文本”中搜索該時間戳記的行號,並從該時間范圍的數據中復制相應的數據部分,以便進行繪制。 此數據以1個樣本/秒的速度收集了8個多小時,因此excel文件中很容易搜索30000行。 大量數據將在圖表上標出不同事件的標簽(假設它們為每個框放了一個事件,但我將if語句考慮在內)。 我現在進行此設置的方式是在gui中,用戶將時間戳記值放置為字符串,然后代碼搜索它們:

if isempty(get(handles.Task16End,'String')) 
IndexTextTask16End  = IndexStart;
else
IndexTextTask16End=strmatch(get(handles.Task16End,'String'),Text,'exact'); %row location for timestamp
end
Task16Span=IndexTextTask16End-IndexTextTask15End; %timespan of this event
Task16LineLocation=Task15LineLocation+ Task16Span/3600; %location for vertical line on graph

因此,我最多可以輸入16個任務,這意味着程序必須在matlab矩陣中的每個可用單元格中搜索這些字符串,直到它遍歷代碼為止。 我怎樣才能更有效地做到這一點? 也許將其設置為搜索,直到找到一個真正的空單元格? 這至少將我的搜索限制為給定數據,而不是整個可能的數組。

如果您正在讀取Excel電子表格中的開始/結束時間並在MATLAB中完成所有其他工作,請考慮在datenum之后將時間轉換為它們的datenum表示 xlsread 這樣,您可以比較數字而不是字符串-更快。 鑒於此,您可以使用邏輯索引來構建所需的數據:

times   = datenum(Text); % assumed Text is just a cell array of times
t_start = datenum(get(handles.StartTime,'String'));
t_end   = datenum(get(handles.EndTime, 'String'));

plotData = Data(times >= t_start & times <= t_end); % note the single &, which is different than &&

strcmp通常比strmatch快很多; 我嘗試了一下,它在我的系統上快得多,不知道為什么(就像快1000倍,我沒想到會有那么大的差異)。

它返回的信息稍有不同- strcmp返回與1邏輯陣列哪里有相匹配的-所以要得到相同的輸出與strmatch只是一個包裝它find

IndexStart=find(strcmp(get(handles.StartTime,'String'),Text)); %start time

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM