Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8516321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:23:40+00:00 2026-06-11T05:23:40+00:00

I have a constructor like this: function p = class_name(folder, file_name) xmlfile = fullfile(folder,

  • 0

I have a constructor like this:

function p = class_name(folder, file_name)

    xmlfile = fullfile(folder, file_name);
    xDoc = xmlread(xmlfile);

        struct = xml2struct(xDoc);

        %lots of repetitive code

end

It reads and XML file and transforms it into a struct. The struct elements are then assign to the class’s properties/fields using some repetitive code like this:

if(isfield(struct.parameters, 'parameter_name'))
    p.property_name = struct.parameters.parameter_name.Text;
else
    p.property_name = '';
end

I would like to put this into a method/function which, given the name of the struct element (here ‘parameter_name’) and the name of the class field/property (here ‘property_name’) assigns the former to the latter.

Is it possible to write a generic function/method for this? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:23:41+00:00Added an answer on June 11, 2026 at 5:23 am

    You can implement such a function using dynamic field names. This is the recommended implementation for such cases:

    function p = setparam(s, p, param_name, prop_name)
        if(isfield(s.parameters, param_name))
           param = s.parameters.(param_name);
           p.(prop_name) = param.Text;
        else
           p.(prop_name) = '';
        end
    end
    

    You can also use setfield and getfield on older versions of MATLAB:

    function p = setparam(s, p, param_name, prop_name)
        if(isfield(s.parameters, param_name))
            param = getfield(s.parameters, param_name);
            p = setfield(p, prop_name, getfield(param, 'Text'));
        else
            p = setfield(p, prop_name, '');
        end
    end
    

    Do not use struct for structure name in your code – it is a reserved keyword.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have lots of code like this in my constructors:- function __construct($params) { $this->property
I have a class constructor like this: public JavoImageCorrectedDataHeader() { ByteBuffer buffer = ByteBuffer.allocate(this.size());
I have a varargs constructor like this : public class Sentence { public String[]
I have a constructor signature like this public NavigationLink(Func<String> getName, Func<UrlHelper, String> getURL, Func<bool>
I have a constructor that looks like this (in c++): Interpreter::Interpreter() { tempDat ==
Let's suppose I have a struct like this: struct my_struct { int a; int
I have a constructor signature that looks like this. public LocateEditorViewModel( ILocateRepository locateRepository, int
I have a Person javascript class like function Person(_name, _id, _salary){ this.Name = _name;
In my Java application I have some copy-constructors like this public MyClass(MyClass src) {
I have a class with two constructors that look like this: public MyClass(SomeOtherClass source)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.