MATLAB Answers

How to speed up plotting from within a loop

조회 수: 134(최근 30일)
Hi, I have a figure which I want to plot all my data to, and am doing in it a loop, it works but is inordinately slow and I think it is down to how I am coding the nested loop, does anyone have any suggestions? I have attached a data file - the code uses a hundred of these and compiles one graph from them.
%%clean up matlab workspace
close all;
clc;
clear all
TestMatrix= [1,1.05000000000000,1.05000000000000;2,1.15500000000000,0.945000000000000;3,1.26000000000000,0.840000000000000;4,1.36500000000000,0.735000000000000;5,1.47000000000000,0.630000000000000;6,1.57500000000000,0.525000000000000;7,1.68000000000000,0.420000000000000;8,1.78500000000000,0.315000000000000;9,1.89000000000000,0.210000000000000;10,1.95000000000000,0.150000000000000; 11.00 1.05 1.05
12.00 0.95 1.16
13.00 0.84 1.26
14.00 0.74 1.36
15.00 0.63 1.47
16.00 0.53 1.57
17.00 0.42 1.68
18.00 0.32 1.78
19.00 0.21 1.89
20.00 0.15 1.95];
Folder_used = dir('/Users/imagexpertinc/Desktop/shelf_moving/stability/*.txt');
files = Folder_used;
amountFILES=numel(files);
numOfTestsDone=20;
legendInfo=zeros(numOfTestsDone,1);
legendInfo=num2cell(legendInfo);
expressionA = 'P\d+';
expressionB = 'T\d+';
%%create a cell array to store results
results=num2cell(nan(amountFILES,9));
vars={'FileName','Pin','floor_time','shelf_time','VelMean','Vel_Std','VolMean','Vol_Std','TestNumber'};
for k=1:amountFILES
opts = detectImportOptions(files(k).name);
file=files(k).name;
data=readtable(files(k).name,opts);
s=size(data,1);
data.Exp_ID=(1:s)';
vel_NS=data.Velocity_ms;
vol_NS=data.Volume_pl;
traj_NS=data.Trajectory_deg;
results{k,1}=(files(k).name);
VelMean=nanmean(vel_NS);
results{k,5}=VelMean;
results{k,6}=nanstd(vel_NS);
VolMean=nanmean(vol_NS);
results{k,7}=VolMean;
results{k,8}=nanstd(vol_NS);
nameOfPin= regexp(file,expressionA, 'match');
nameOfPin=nameOfPin{:};
Pin=strrep(nameOfPin,'P','');
Pin=str2double(Pin);
results{k,2}=Pin;
TestNumber=regexp(file,expressionB, 'match');
TestNumber=TestNumber{:};
Test=strrep(TestNumber,'T','');
Test=str2double(Test);
results{k,3}=TestMatrix(Test,2);
results{k,4}=TestMatrix(Test,3);
results{k,9}=Test;
left_color = [0 0 0];
right_color = [0 0 0];
end
resultsDouble=cell2mat(results(:,2:9));
%%Make a Table
results_Table=cell2table(results);
results_Table.Properties.VariableNames=vars;
results_Table = sortrows(results_Table,9);
hFig=figure('units','normalized','outerposition',[0 0 1 1]);
for k=1:amountFILES
for j=1:numOfTestsDone
subTable = resultsDouble(:,8)==j;
subData= resultsDouble(subTable,:);
shelf_time_label=subData(1,3);
legendInfo{j} = ['Shelf time ',num2str(shelf_time_label),'uS'];
for m=1:size(subData,1)
set(0,'CurrentFigure',hFig)
scatter(subData(:,1),subData(:,5),'filled')
hold on
end
end
end
lgd=legend(legendInfo);
lgd.FontSize=15;
lgd.FontWeight ='bold';
hold on
ylabel('Standard Deviation of Velocity','FontSize',15,'FontWeight','bold')
xlabel('Nozzle number','FontSize',15,'FontWeight','bold')
title('Velocity Stability','FontSize',15,'FontWeight','bold')
box on
grid minor
print('Velocity_Stability', '-dpng','-r0')

  댓글 수: 6

표시 이전 댓글 수: 3
Stephen Devlin
Stephen Devlin 4 Jun 2018
Thanks Steve
Stephen Devlin
Stephen Devlin 4 Jun 2018
Got it, my first loop in the nested loop was doing nothing, only takes a second once that is gone and removes a 100x multiplier of the loop. Thanks Steve
Stephen Devlin
Stephen Devlin 4 Jun 2018
Steve if you move this into the answer section I will accept it.

로그인 to comment.

채택된 답변

Stephen Cobeldick
Stephen Cobeldick 5 Jun 2018
You should use the profiler to find where the slow parts of the code are:
Note that that your code must be in functions for the profiler to work. A convenient way to call the profiler is:
profile on
... call your function/s
profile viewer
You should also read this very carefully:

  댓글 수: 0

로그인 to comment.

추가 답변(0개)

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by