about a ChemName function

1 vue (au cours des 30 derniers jours)
eli manasherov
eli manasherov le 15 Mar 2020
Modifié(e) : dpb le 15 Mar 2020
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
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
eli manasherov le 15 Mar 2020
function Out=ChemName(In)
localChemNameStr=In.ChemName;
Out=localChemNameStr;
comp=Out.ChemName;
comp=reshape(comp,1:legth(comp));
comp=trenspose(comp);
comp=d13line(comp);
comp=strtrim(comp);
end
I did that and he is telling me that the last comp is unused.
Why is that?
dpb
dpb le 15 Mar 2020
Modifié(e) : dpb 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by