Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Why does this produce an error?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Rik
am 8 Dez. 2018
Function definitions should not contain values, but should contain variable names. Also, your input doesn't seem to be used in any way.
Stephen23
am 9 Dez. 2018
Bearbeitet: Stephen23
am 9 Dez. 2018
"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).
Antworten (3)
Greg Heath
am 9 Dez. 2018
Remove the square brackets in the first line
Thank you for formally accepting my answer
Greg
1 Kommentar
Rik
am 9 Dez. 2018
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
am 9 Dez. 2018
https://www.mathworks.com/matlabcentral/answers/433934-how-to-pix-the-syntax-error#answer_350643
0 Kommentare
Elijah Smith
am 9 Dez. 2018
Bearbeitet: Elijah Smith
am 9 Dez. 2018
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 Kommentare
Elijah Smith
am 10 Dez. 2018
good point, but I dont have that information so I am also only assuming.
Walter Roberson
am 10 Dez. 2018
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.
Diese Frage ist geschlossen.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!