0

I'm trying to create an array of struct and prompt the user to fill its fields individually.

book =struct('name',{''}, 'author',{''}, 'pubdate',{''}, 'price',{})
nmbr=input('enter number of books');
books = repmat(book, nmbr, 1);

for i=1:nmbr
    books(i).name = input(sprintf('enter name of book #(%d)',i), 's')
    books(i).author = input(sprintf('entre author of book #(%d)',i), 's')
    books(i).pubdate = input(sprintf('enter publication date of book #(%d)',i), 's')
    books(i).price = input(sprintf('enter price of book #(%d)',i))
end

However, I'm missing something as I keep getting this error:

??? Attempt to call constructor struct with incorrect letter case.


Error in ==> struct at 1
book =struct('name',{''}, 'author',{''}, 'pubdate',{''}, 'price',{})

I tried declrng the struct differently

book =struct('name','', 'author','', 'pubdate','', 'price','')

But I keep getting the same error for some reason.

Any ideas on what I'm doing wrong would be appreciated. Thank you.

0

1 Answer 1

2

MATLAB is pretty verbose and clear on the errors that you get when you're programming.

Look very carefully at your error:

Error in ==> struct at 1
book =struct('name',{''}, 'author',{''}, 'pubdate',{''}, 'price',{})

struct at 1 means that it found an error at line 1 of your file called struct. You unintentionally named your file struct.m when struct is also the method to create struct variables. Therefore, MATLAB is confused because there are two definitions of struct: Your file name as well as struct being part of MATLAB's native calls.

You need to change the name of your file and run your code again. When I did that, your code worked for me, though there are some unnecessary output printing. I suggest you place semi-colons at the end of each statement within your for loop so that it's less distracting.


If your file is different, then either of the following happened:

  • You're putting a bad function declaration. You may have placed a function declaration at the top of your code... something like function struct, or something of that nature and that's what why it's complaining
  • Your workspace has a variable called struct somewhere and so you are trying to call this variable rather than creating a struct.
  • You have a file called struct declared somewhere in your working directory or the current MATLAB path.

Also, @excaza noticed that the error is due to improper case. You probably have a file or variable or something called Struct (with a capital S) somewhere and you need to remove that.

Try clearing your workspace by doing clear all;, then try your code again. Also, make sure there isn't a file called struct.m somewhere in your working directory of MATLAB's system path. Try doing which struct and see what you get in the Command Prompt. You should see that it's built-in and it directs you to the matlab/datatypes/struct folder. If not, then go to where MATLAB is pointing you to, rather than native MATLAB folder and delete that file.

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

9 Comments

Is the first line of your code function struct, or something of that nature? That error is still very clear. MATLAB thinks your file is called struct. Try clearing all variables and run your code again. Do clear all;.
@iksimen - I just ran this code on my end and it works. There's something you're not telling us when you created that file. You're either putting a bad function declaration, or your workspace has a variable called struct somewhere, or you have a file called struct declared somewhere in your working directory or the current MATLAB path. Either way, it's nothing that I can reproduce because you haven't given me everything.
@rayryeng or there's a separate file(s) in the current directory or the path named struct
@rayryeng well, more likely named Struct since it's talking about capitalization :p
There must be a class somewhere that's using stackoverflow as a TA. This is the third person with the exact same homework. ;)
|

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.