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

조회 수: 98 (최근 30일)
Ibro Tutic
Ibro Tutic 2017년 5월 11일
댓글: Rena Berman 2021년 5월 6일
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
Rena Berman
Rena Berman 2021년 5월 6일
(Answers Dev) Restored edit

댓글을 달려면 로그인하십시오.

채택된 답변

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