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