matlab struct array 和 cell array的区别



matlab struct array 和 cell array的区别struct 是具有属性名的 也就是field, 所以对每个struct 根据这个field 来获取内容。下面的例子从matlab官网来,可见通过.test ,.name(红色部分)这样的名字来获得相应的元素。

For example, store patient records in a structure array.

 patient(1).name = 'John Doe'; patient(1).billing = 127.00; patient(1).test = [79, 75, 73; 180, 178, 177.5; 220, 210, 205]; patient(2).name = 'Ann Lane'; patient(2).billing = 28.50; patient(2).test = [68, 70, 68; 118, 118, 119; 172, 170, 169];

Create a bar graph of the test results for each patient.

 numPatients = numel(patient); for p = 1:numPatients figure bar(patient(p).test) title(patient(p).name) end
 


 
cell    没有属性名, 通过index对 来获取其值。下面的例子从matlab官网来,可见通过1,2,3 (红色部分)这样的index来获得相应的元素。

For example, store temperature data for three cities over time in a cell array.

 temperature(1,:) = {'01-Jan-2010', [45, 49, 0]}; temperature(2,:) = {'03-Apr-2010', [54, 68, 21]}; temperature(3,:) = {'20-Jun-2010', [72, 85, 53]}; temperature(4,:) = {'15-Sep-2010', [63, 81, 56]}; temperature(5,:) = {'31-Dec-2010', [38, 54, 18]};

Plot the temperatures for each city by date.

 allTemps = cell2mat(temperature(:,2)); dates = datenum(temperature(:,1), 'dd-mmm-yyyy'); plot(dates,allTemps)