MATLAB Answers

0

Why does this produce an error?

Asked by Delaney Andersen on 8 Dec 2018 at 18:10
Latest activity Commented on by Walter Roberson
on 10 Dec 2018 at 7:01
I am trying to create a function for a structure; I am not sure what is wrong with my syntax to where this is producing an error (this is the way I was taught it). MATLAB says that there is an error in the first function line:
MyBks = GetArrayBkStruct_('Title','ISBN','numberPages');
function[MyBks]=GetArrayBkStruct_('Title','ISBN','numberPages')
MyBks(1) = aBkStruct_('Neural Networks - Systematics','978-3-642-61068-4',502);
MyBks(2) = aBkStruct_('The Journey of Man - Genetics','978-0-691-11532-X',224);
MyBks(3) = aBkStruct_('Introduction to Neural Networks','978-3-642-57760-4',331);
MyBks(4) = aBkStruct_('Neural Networks Methodology','978-3-540-28847-3',498);
MyBks(5) = aBkStruct_('The Lost Symbol','978-1-4000-7914-8',639);
MyBks(6) = aBkStruct_('Neural Networks in Biomedicine','978-1-4471-0487-2',288);
MyBks(7) = aBkStruct_('Neural Networks: Computations','978-3-540-69226-3',300);
end

  2 Comments

Function definitions should not contain values, but should contain variable names. Also, your input doesn't seem to be used in any way.
"I am not sure what is wrong with my syntax to where this is producing an error"
But you could find out easily enough by reading the MATLAB documentation on how to define functions. The documentation explains how to use MATLAB: it is easy to search using your favorite internet search engine, and is faster than asking a question on an internet forum and waiting for random strangers to help you.
"(this is the way I was taught it)."
You should demand your money back (unless your teacher gave a lesson on how to write bugs).

Sign in to comment.

Products


Release

R2018b

3 Answers

Answer by Greg Heath
on 9 Dec 2018 at 7:03

Remove the square brackets in the first line
Thank you for formally accepting my answer
Greg

  1 Comment

Even if there should be a space between the function keyword and the bracket, I expect Matlab to be able to parse that. However, any char array as an input will absolutely cause an error.

Sign in to comment.


Answer by Walter Roberson
on 9 Dec 2018 at 8:33

https://www.mathworks.com/matlabcentral/answers/433934-how-to-pix-the-syntax-error#answer_350643

  0 Comments

Sign in to comment.


Answer by Elijah Smith on 9 Dec 2018 at 18:11
Edited by Elijah Smith on 9 Dec 2018 at 18:12

Here are the problems:
1) you can't use a value for a input (as the other people have said)
2) even if your inputs were variables, you don't use them anywhere. (as one guy mentioned)
3) even if you wrote your code like this (without syntax errors):
function [MyBks] =GetArrayBkStruct_(Title,ISBN,numberPages)
MyBks(1) = aBkStruct_('Neural Networks - Systematics','978-3-642-61068-4',502);
MyBks(2) = aBkStruct_('The Journey of Man - Genetics','978-0-691-11532-X',224);
MyBks(3) = aBkStruct_('Introduction to Neural Networks','978-3-642-57760-4',331);
MyBks(4) = aBkStruct_('Neural Networks Methodology','978-3-540-28847-3',498);
MyBks(5) = aBkStruct_('The Lost Symbol','978-1-4000-7914-8',639);
MyBks(6) = aBkStruct_('Neural Networks in Biomedicine','978-1-4471-0487-2',288);
MyBks(7) = aBkStruct_('Neural Networks: Computations','978-3-540-69226-3',300);
end
your function still won't work because you haven't defined the variable (or struct) "aBkStruct." I assume that you have defined this variable elsewhere but recall that matlab functions only see variables that are defied within the function workspace.

  3 Comments

II would presume that aBkStruct_ is a function that constructs a struct.
good point, but I dont have that information so I am also only assuming.
it is uncommon to index a variable with a character vector but there can be good reason to do so. but the result would not be a scalar so it could not be stored in aa scalar location .
However if the name corresponds to a function that constructs a struct entry then it all works out.

Sign in to comment.