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

101 visualizaciones (últimos 30 días)
Ibro Tutic
Ibro Tutic el 11 de Mayo de 2017
Comentada: Rena Berman el 6 de Mayo de 2021
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 comentarios
Stephen23
Stephen23 el 26 de Dic. de 2020
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 el 6 de Mayo de 2021
(Answers Dev) Restored edit

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 11 de Mayo de 2017
Editada: Stephen23 el 11 de Mayo de 2017
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 comentarios
Ibro Tutic
Ibro Tutic el 11 de Mayo de 2017
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 el 11 de Mayo de 2017
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by