user2133827
user2133827

Reputation:

Matlab: Invalid field name for structure array

Basically, my programs is supposed to accept votes, saved by name of the voter id, until the secret id is entered, at which point it will display the id. It has to be saved as a structure array. Here is what I've got so far:

secretid = 123456789;
currid = 0;
while currid ~= secretid
  currid = input('Enter your id:');
  if currid == secretid
    votedata
    break
  else
    strcurrid = num2str(currid);
    vote = input('Enter your vote:', 's');
    votedata = struct(strcurrid, vote);
  end
end

According to Matlab, the field name is invalid - it must be saved as a string, but I don't understand, I converted currid to a string, and don't see why it isn't working.

Upvotes: 0

Views: 13163

Answers (1)

Dedek Mraz
Dedek Mraz

Reputation: 814

from Matlab's struct reference page:

field — Field name

string

Field name, specified as a string. Valid field names begin with a letter, and can contain letters, digits, and underscores. The maximum length of a field name is the value that the namelengthmax function returns.

I would suggest adding a dummy letter at the beginning of your id, or just using:

strcurrid = ['id', num2str(currid)];

Upvotes: 6

Related Questions