Info
此问题已关闭。 请重新打开它进行编辑或回答。
Why does this produce an error?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Rik
2018-12-8
Function definitions should not contain values, but should contain variable names. Also, your input doesn't seem to be used in any way.
Stephen23
2018-12-9
编辑:Stephen23
2018-12-9
"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).
回答(3 个)
Greg Heath
2018-12-9
Remove the square brackets in the first line
Thank you for formally accepting my answer
Greg
1 个评论
Rik
2018-12-9
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.
Walter Roberson
2018-12-9
https://www.mathworks.com/matlabcentral/answers/433934-how-to-pix-the-syntax-error#answer_350643
0 个评论
Elijah Smith
2018-12-9
编辑:Elijah Smith
2018-12-9
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 个评论
Walter Roberson
2018-12-10
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.
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!