about a ChemName function
Afficher commentaires plus anciens
Hello there
I want to write a function which the input is HeterogenName type structure, that one of is fields is ChemName.
And the output is a structure with same field names, that in ChemName field there is an array of characters (of a line).
I don't know how to do ChemName use here.
Yhank you.
Réponses (1)
dpb
le 15 Mar 2020
You just pass the struct as the argument to the function and return the same struct; there's nothing required in MATLAB to define what the arguments are--
function Out=ChemName(In)
localChemNameStr=In.ChemName; % reference input struct field
...
Out=In; % copy input to output
Out.ChemName='YourNewCompoundName'; % define the new name for the output/returned struct
end
4 commentaires
eli manasherov
le 15 Mar 2020
"I dont think you understand what i meant,..."
Au contraire! I think I hit the nail almost squarely on the head... :)
Other than having no way to know the field(s) in the struct might be cell arrays and the specific manipulations requested in iii) 2-5, the prototype function is the requested function outline for you to finish the details.
Since now this is clearly homework, we'll need to see your efforts to do the work and will stand by for specific questions where you subsequently get stuck.
W/o knowing what the content of the input structure actually is, there's certainly some ambiguity in what the numbered points 2) thru 5) might specifically entail, but undoubtedly you've been provided a sample struct with some data to go with it for the purpose.
eli manasherov
le 15 Mar 2020
My sample code other than the one line you didn't keep that did the very first thing your assignment asked/told you to do was just that...sample code!
Write your own version using meaningful variable names.
You need to explore what is actually in the passed-in ChemName field; there's no way to know from here what that might be nor what,precisely, the instructor meant by "the cells" in step (2); I've found even less-than-expert instructors confuse the elements of an array with the word "cell" aka a spreadsheet paradigm where as in MATLAB, a cell is a cellarray data structure which can contain virtually anything.
So, you need to see what is actually meant here -- we can't see your terminal nor workspace from here.
In the above, all you've assigned to the output variable is the one field, NOT the full struct as is requested.
After that, you've reassigned the variable comp multiple times and have various issues therein as well including a typo in "trenspose" for transpose and an undefined variable/function d13line
The biggie is, however, that you have referred to the variable comp which is a copy of whatever is the content of the ChemName field in the input struct, but never assign that result to the output struct as requested.
And, of course, that you have an output structure that consists of only the one field name--presuming there were other fields defined in the struct passed to the function, they've not been copied to the out per the assignment request.
Catégories
En savoir plus sur Structures dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!