簡體   English   中英

MatLab:parfeval() 不調用 function

[英]MatLab: parfeval() doesn't call function

我正在嘗試接近實時地處理來自 UDP 服務器的數據。

For that I wrote this MatLab Code to fill a Buffer with the UDP Datagrams and Process the Data (split the strings, etc.) once the Buffer from my MatLab function is full (myBuffer).

在處理數據期間(大約需要 0.9 秒),我需要在接收數據時 go 並將它們存儲在(現在)清空的緩沖區中。

我發現“並行計算工具箱”的 parfeval-Function 可能適合我的需要,因為我需要 function “ProcessData”在后台運行。

我遇到的問題是我無法讓它運行,因為 parfeval function 沒有進入我的 function ProcessData。 我通過在 ProcessData() 中設置斷點對其進行了測試,但程序永遠不會停止。 我對 function 參數做錯了嗎?

That's what MatLab help says: F = parfeval(p,fcn,numout,in1,in2,...) requests asynchronous execution of the function fcn on a worker contained in the parallel pool p, expecting numout output arguments and supplying as input arguments in1,in2,....

希望你們能幫我解決這個問題。 提前致謝。

function ReadVoltage

    %% Specify a Server (host name or IP address) with Port 8080
    u = udp('192.168.0.164', 8080);  %UDP Object Zuhause
    %u = udp('169.254.38.221', 8080);  %UDP Object Pilotfabrik    

    % Buffer in the enclosing function
    myBuffer = {};  %Initialisierung
    MAXBUFFLEN = 100; %Maximale Anzahl an Eintraegen in Buffer (1 Eintrage = 1 Datagram)

    u.InputBufferSize = 4060; 
    u.ReadAsyncMode = 'continuous';
    u.DatagramReceivedFcn = @DatagramReceivedFcn;
    u.ErrorFcn = @ErrorFcn;
    u.DatagramTerminateMode = 
    u.Terminator = '!';

    %% Initialize Parallel pool 
    pool = gcp();

    %% Oeffnen der Verbindung
    fopen(u);

     if (~strcmp(u.Status,'open'))
         NetworkError(u,'Connection failed!');
     end


    %% Start Data transmission by trigger
    fprintf(u, 'Requesting Data')

    %% Callback Funktion
    function DatagramReceivedFcn(u,~) 

        datagram = fscanf(u);
        disp('Data Received!');


        myBuffer{end+1} = datagram;         %Appends datagram to buffer


        [~, bufflen] = size(myBuffer);

        if bufflen < MAXBUFFLEN
            return;
        else
            f = parfeval(pool, @ProcessData, 1, myBuffer);
            myBuffer = {};               %empty Buffer
        end 

    end

    function ErrorFcn(u,~) 
        disp("An Error occured");
    end

end

function datagram_values = ProcessData(myBuffer)


    stringvalues = split(myBuffer, ";");        %Split Strings
    doublevalues = str2double(stringvalues)     %Convert Strings do Doubles

    dim_doublevalues = size(doublevalues);      %Dimension of Double Output Array

    i_max = dim_doublevalues(2)                 %Anzahl der Datenpakete
    j_max = (dim_doublevalues(3))-1             %Anzahl der Werte pro Datenpaket; -1 wegen leerem Wert nach ";" am Ende
    k_max = i_max*j_max                         %Gesamtanzahl der Werte in Buffer

    k=1;
    while k<=k_max
        for i = 1:i_max
            for j = 1:j_max
                    datagram_values(k,1)=doublevalues(1,i,j);
                    k=k+1;
            end
        end
    end

    disp(datagram_values);


end

不幸的是,MATLAB 調試器無法停止在工作程序上運行的代碼 - 只有在客戶端運行的代碼。

在這種情況下,您應該嘗試查看未來f的日記 output ,如下所示:

f = parfeval(...);
wait(f); % wait for the worker to complete
disp(f.Diary); % display the output

如果您不想阻止客戶端,可以使用afterEach調用對disp的調用,如下所示:

f = parfeval(...);
afterEach(f, @(f) disp(f.Diary), 0, 'PassFuture', true);

暫無
暫無

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

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