You did not define your function to have any output:
function []=filesort(x)
^^ no output argument!
Also your code is much longer and much more complicated than it needs to be. Try this (I just tested it):
function otpS = filesort(inpS)
C = {'Z1P','Z2P','Z1G','Z2G','Z3K','Z4K','Z5K','Z3M','Z4M','Z5M','Z6MS','Z7PR','Z7PR1'};
N = {inpS.name};
otpS = struct();
for k = 1:numel(C)
idx = cellfun('isempty',regexp(N,C{k},'once'));
otpS.(C{k}) = inpS(~idx);
end
end
Tip: computers are only good at doing one thing: repeatedly doing small stupid tasks. So when you find yourself copy-and-pasting code like that, and repeating the same code over and over again, then you are doing the computer's job. Instead of wasting your life, get the computer to do it: use a loop.
1 Comment
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/339839-how-to-get-a-structure-as-an-output-from-a-function#comment_1228167
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/339839-how-to-get-a-structure-as-an-output-from-a-function#comment_1228167
Sign in to comment.