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

104 ビュー (過去 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 Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by