Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Why does this produce an error?

1 回表示 (過去 30 日間)
Delaney Andersen
Delaney Andersen 2018 年 12 月 8 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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
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
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
Greg Heath 2018 年 12 月 9 日
Remove the square brackets in the first line
Thank you for formally accepting my answer
Greg
  1 件のコメント
Rik
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
Walter Roberson 2018 年 12 月 9 日
https://www.mathworks.com/matlabcentral/answers/433934-how-to-pix-the-syntax-error#answer_350643

Elijah Smith
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 件のコメント
Elijah Smith
Elijah Smith 2018 年 12 月 10 日
good point, but I dont have that information so I am also only assuming.
Walter Roberson
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.

この質問は閉じられています。

製品


リリース

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by