How to declare multiple output arguments ?
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ioannis Agalliadis
am 4 Jul. 2017
Kommentiert: Jan
am 6 Jul. 2017
I have declared in one .m file all the following output arguments for my function preprocessing_data. The input arguments are the number of the samples gathered for each movement that I am investigating.
Here is what it looks like:
function [Ext, Flx, Stft_ext, Stft_flx,Rms_ext, Rms_flx,...
Smoothed_ext, Smoothed_flx, Haar_ext, Bior13_ext, Db8_ext,...
Bior22_ext, Sym4_ext, Coif3_ext,Sym8_ext, Coif4_ext,...
Haar_flx, Bior13_flx, Db8_flx, Bior22_flx, Sym4_flx,...
Coif3_flx, Sym8_flx, Coif4_flx, Amor_ext, Bump_ext, Morse_ext] = preprocessing_data(Ext_dim,Flx_dim)
It seems to me that this is not the right way to declare output argument and there is a more elegant way of doing this. Another problem with that is that I do not get the output arguments is spite of having them declared.
Could someone guide me how to fix this ?
Thanks in advance!
7 Kommentare
Adam
am 4 Jul. 2017
"I tried to invoke by typing output.name(2)"
That is what he is talking about - it is the key detail and you never mentioned it previously.
You created a struct array with 1 field called 'name' in each element of the struct.
This needs to be accessed as:
output(2).name;
as you would see from examining the struct you created.
Akzeptierte Antwort
Jan
am 4 Jul. 2017
Bearbeitet: Jan
am 4 Jul. 2017
It seems to me that this is not the right way to declare output argument
I agree. Although it is syntactically correct and efficient for processing, it is painful to read. If the code contains a typo, e.g. swapped variables, it will be hard to find the problem.
I do not get the output arguments
Without further explainations this is not clear. If a fubnction is declared as:
function [a,b] = fcn(c)
a = c + 1;
b = c * 2;
calling it as
a = fcn(c)
replies a only, and
[a,b] = fcn(c)
replies both values. I cannot guess, why you do not get the outputs as expected.
I suggest to combine the values in a struct:
function Out = ...
preprocessing_data(Ext_dim,Flx_dim)
Out.Ext = ...
Out.Flx = ...
Out.Stft.Ext = ...
Out.Stft.Flex = ...
...
end
Alternatively:
Out.Ext(1).Value = ...
Out.Ext(1).Name = 'RMS';
Out.Flex(1).Value = ...
Out.Flex(1).Name = 'RMS';
Out.Ext(2).Value = ...
Out.Ext(2).Name = 'Stft';
...
Find a common structure for the data. Reply arrays or a struct array, which combines similar or equivalent data. Keep the information about the source or category of the data in a dedicated struct field. This is less prone to typos and easier to expand: Adding a new field or array element does not required to change the code in the caller. You can process all parameters using a loop instead.
9 Kommentare
Jan
am 6 Jul. 2017
@Ioannis Agalliadis: The error message is clear. For the corresponding i the file does not exist. This problem does not belong to the original question and the readers in the forum cannot guess, which files are existing. Use dir() to get the existing files.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Spreadsheets finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!