How to Get a Structure as an Output from a Function?

98 次查看(过去 30 天)
I have this function made to sort data into a structure. However, when I call a directory as the input, I can't seem to get an output. I call it by doing
filesort(dir([foldername(1).standard,'\*.dat'])).
However, when I set x=sortdata(dir([foldername(1).standard,'\*.dat'])), within the function and run it, I get my desired Srt structure.
Any ideas?
This is the function:
function []=filesort(x)
C = {'Z1P'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z1P = S(idx);
C = {'Z2P'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z2P = S(idx);
C = {'Z1G'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z1G = S(idx);
C = {'Z2G'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z2G = S(idx);
C = {'Z3K'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z3K = S(idx);
C = {'Z4K'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z4K = S(idx);
C = {'Z5K'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z5K = S(idx);
C = {'Z3M'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z3M = S(idx);
C = {'Z4M'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z4M = S(idx);
C = {'Z5M'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z5M = S(idx);
C = {'Z6MS'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z6MS = S(idx);
C = {'Z7PR'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z7PR = S(idx);
C = {'Z7PR1'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z7PR1 = S(idx);
end
  2 个评论
Stephen23
Stephen23 2020-12-26
Original question by Ibro Tutic on 11th of May 2017 retrieved from Google Cache:
How to Get a Structure as an Output from a Function?
I have this function made to sort data into a structure. However, when I call a directory as the input, I can't seem to get an output. I call it by doing
filesort(dir([foldername(1).standard,'\*.dat'])).
However, when I set x=sortdata(dir([foldername(1).standard,'\*.dat'])), within the function and run it, I get my desired Srt structure.
Any ideas?
This is the function:
function []=filesort(x)
C = {'Z1P'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z1P = S(idx);
C = {'Z2P'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z2P = S(idx);
C = {'Z1G'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z1G = S(idx);
C = {'Z2G'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z2G = S(idx);
C = {'Z3K'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z3K = S(idx);
C = {'Z4K'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z4K = S(idx);
C = {'Z5K'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z5K = S(idx);
C = {'Z3M'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z3M = S(idx);
C = {'Z4M'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z4M = S(idx);
C = {'Z5M'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z5M = S(idx);
C = {'Z6MS'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z6MS = S(idx);
C = {'Z7PR'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z7PR = S(idx);
C = {'Z7PR1'};
S = x;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{x.name});
Srt.Standard(1).Z7PR1 = S(idx);
end

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2017-5-11
编辑:Stephen23 2017-5-11
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.
  3 个评论
Ibro Tutic
Ibro Tutic 2017-5-11
That is exactly what I was trying to on my own, but couldn't get it to work. Thanks a lot for the help.
KL
KL 2017-5-11
Stephen has assigned otpS as the function output, if you call from command line
y = filesort(x)
then the function return will be saved under y.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by