1

This question is more conceptual on how to progress with code.

In MATLAB the struct function allows for people to make an "object"-like entity called a struct and then to add extra layers or dimensions

call the structname(n+1).fieldname=new_input_field_value; and then that adds a whole other layer.

The problem is though- that maybe people could add a value that is not intended.

What I Have: For my struct template

table= struct("filename", " ", "F1", 0, "F2", 0, "F3", 0); 

I want to continually add filenames, and the values for F1 --> F3. Filenames will be called from an existing folder. And F1 and F3 are calculated using DSP calculations, and will be some sort of number data-type.

For my purposes, I will only add in integer or float values for F1-->F3.

But in manual testing, you can add in anything for any of the fields, strings, numbers --- everything is valid. <-- what I want to avoid

2
  • 2
    If you need to validate data written to the struct, you'll have to create a custom class. They can work exactly the same way as a struct, but additionally have member functions as well as setter and getter functions that get called for the name(i).field syntax. You should start here. Commented Apr 3, 2019 at 22:03
  • 2
    Alternatively, you could use a table object, where each column can have a specific type. The syntax is a little different, and indexing is a little slower, but it might do what you need. Commented Apr 3, 2019 at 22:06

1 Answer 1

1

You can check for the type of variable before the adding operation, so instead of:

StructName.FieldName = yourVariable;

Do this:

if (isnumeric(yourVariable))
     StructName.FieldName = yourVariable;
end

Is this what you need?

Sign up to request clarification or add additional context in comments.

1 Comment

I was thinking that could work. My question was more along the lines of, is there a way to define- when you call the struct, what types are valid. But based on what people are saying in the comments above, it seems that in MATLAB you can do that by writing a class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.