about a ChemName function

1 次查看(过去 30 天)
eli manasherov
eli manasherov 2020-3-15
编辑: dpb 2020-3-15
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.

回答(1 个)

dpb
dpb 2020-3-15
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 个评论
eli manasherov
eli manasherov 2020-3-15
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 2020-3-15
编辑:dpb 2020-3-15
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by