0

I've done a simple Matlab class

classdef WavReader
...

and I am trying to use it:

wr = WavReader(in_path);
wr.SetChunkLength( block_size);
while true    
    out = wr.ReadChunk();

However, this doesn't work: some class properties are "forgotten" before the loop is executed, resulting in an error.

If I defined a class as a handle class

classdef WavReader < handle
...

all works perfectly well.

Where did I go wrong with the value class?

Danijel
  • 8,198
  • 18
  • 69
  • 133
  • i feel like everything is working fine, but the very concept of the values class is that each command works on its own copy of it. If you set the blocksize on a Valueclass WavReader it is created the blocksize is set and then its deleted again. If you do the same thing on a handle class the object is created and modified and later used. If you want to use it as value class `SetChunkLength( block_size)` has to happen inside `wr.ReadChunk()` but i feel like handle class would be the better approach here – Finn Nov 07 '16 at 09:02
  • I am not using `handle` class because this seems like a very good case for `object` class since I'd probably need several `WavReaders` to read severa wav files at the same time. – Danijel Nov 07 '16 at 10:34
  • ok then! your code should be like `out = wr.ReadChunk(block_size);` if that is the only input needed. If you want help with that could you please add your code or is this topic closed? – Finn Nov 07 '16 at 11:31
  • If I understand correctly, there is no way for a method to change some internal class property, so that another method can use this changed property? – Danijel Nov 07 '16 at 11:43
  • for *value* classes: no, for *handle* classes: yes. that would be the very difference of those two [see Mathworks comparison](https://de.mathworks.com/help/matlab/matlab_oop/comparing-handle-and-value-classes.html). The only way you could handle (intended pun) that is if you set the value in the creation of the value object `wr = WavReader(in_path,block_size)`. it still couldn't be changed while running, but you could influence the object property at set it to different values for multiple objects. – Finn Nov 07 '16 at 12:11
  • OK, so in case of a "value" class, everything that needs to be changed should be set within a constructor. – Danijel Nov 07 '16 at 13:09
  • yes. is your question answered with that or do you want support in writing the corresponding code? – Finn Nov 07 '16 at 14:29
  • Additionally, to answer you second comment handle class does not mean singleton. You can have multiple instances of them each with its own properties. – Navan Nov 07 '16 at 15:11
  • @Navan Very good point - I didn't know that. In case I can have multiple instances with handle classes, my problem is solved. – Danijel Nov 07 '16 at 15:17

0 Answers0