www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/ShrlibCreateCellArrayFromLibpointerObjectExample.m

    %% Create Cell Array from lib.pointer Object
% This example shows how to create a MATLAB® cell array of character vectors, |mlStringArray|, 
% from the output of the |getListOfStrings| function. 
% 
% Load the |shrlibsample| library.

if not(libisloaded('shrlibsample'))
    addpath(fullfile(matlabroot,'extern','examples','shrlib'))
    loadlibrary('shrlibsample')
end
%% 
% Call the |getListOfStrings| function to create an array of character vectors. The 
% function returns a pointer to the array.

ptr = calllib('shrlibsample','getListOfStrings');
class(ptr)
%% 
% Create indexing variables to iterate through the arrays. Use |ptrindex| 
% for the array returned by the function and |index| for the MATLAB array.

ptrindex = ptr;
index = 1;
%% 
% Create the cell array of character vectors |mlStringArray|. Copy the output of |getListOfStrings| 
% to the cell array.

% read until end of list (NULL)
while ischar(ptrindex.value{1}) 
    mlStringArray{index} = ptrindex.value{1};
    % increment pointer 
    ptrindex = ptrindex + 1; 
    % increment array index
    index = index + 1; 
end
%% 
% View the contents of the cell array.

mlStringArray
%% 
%