How to Get a Structure as an Output from a Function?
조회 수: 46 (최근 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
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
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
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 Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!