Guest User

Untitled

a guest
Aug 18th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. Headers in dataset (Matlab)
  2. dmsdb = dataset({ 'John','Name'},{'Amsterdam','City'},{10,'number' });
  3. produces:
  4. Name City number
  5. John Amsterdam 10 --> Headers are good!
  6.  
  7. dmsdb(1,1:3) = dataset({ cellstr('John'),'Name'},{cellstr('Amsterdam'),'City'},{10,'number' });
  8. Produces:
  9. Var1 Var2 Var3
  10. 'John' 'Amsterdam' 10
  11.  
  12. data = dataset({[], 'Name'}, {[], 'City'}, {[], 'number'});
  13.  
  14. data = dataset([], [], [], 'VarNames', {'Name', 'City', 'number'});
  15.  
  16. >> data
  17.  
  18. data =
  19.  
  20. [empty 0-by-3 dataset]
  21.  
  22. >> get(data, 'VarNames')
  23.  
  24. ans =
  25.  
  26. 'Name' 'City' 'number'
  27.  
  28. >> data = [data; dataset({'John'}, {'Amsterdam'}, 10, 'VarNames', get(data, 'VarNames'))]
  29.  
  30. data =
  31.  
  32. Name City number
  33. 'John' 'Amsterdam' 10
  34.  
  35. >> dmsdb = dataset({ {'John'},'Name'},{{'Amsterdam'},'City'},{10,'number' });
  36.  
  37. dmsdb =
  38.  
  39. Name City number
  40. 'John' 'Amsterdam' 10
  41.  
  42. %# create dataset with no rows
  43. ds = dataset(cell(0,1),cell(0,1),zeros(0,1));
  44. ds.Properties.VarNames = {'Name', 'City', 'number'};
  45.  
  46. %# adding one row at a time
  47. for i=1:3
  48. row = {{'John'}, {'Amsterdam'}, 10}; %# construct new row each iteration
  49. ds(i,:) = dataset(row{:});
  50. end
  51.  
  52. %# adding a batch of rows all at once
  53. rows = {{'Bob';'Alice'}, {'Paris';'Boston'}, [20;30]};
  54. ds(4:5,:) = dataset(rows{:});
  55.  
  56. >> ds
  57. ds =
  58. Name City number
  59. 'John' 'Amsterdam' 10
  60. 'John' 'Amsterdam' 10
  61. 'John' 'Amsterdam' 10
  62. 'Bob' 'Paris' 20
  63. 'Alice' 'Boston' 30
  64.  
  65. vars = {'Name', 'City', 'number'};
  66. ds = [ds ; dataset(rows{:}, 'VarNames',vars)]
  67.  
  68. % 1) Create the 3 variables of interest, then make the dataset.
  69. % Make sure they are column vectors!
  70. >> Name = {'John' 'Joe'}'; City = {'Amsterdam' 'NYC'}'; number = [10 1]';
  71. >> dataset(Name, City, number)
  72.  
  73. ans =
  74.  
  75. Name City number
  76. 'John' 'Amsterdam' 10
  77. 'Joe' 'NYC' 1
  78.  
  79. % 2) More compact than doing 3 separate cell arrays
  80. >> dataset({{'John' 'Amsterdam' 10} 'Name' 'City' 'number'})
  81.  
  82. ans =
  83.  
  84. Name City number
  85. 'John' 'Amsterdam' [10]
Add Comment
Please, Sign In to add comment