How to declare multiple output arguments ?

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 Commenti

Stephen23
Stephen23 il 4 Lug 2017
Modificato: Stephen23 il 4 Lug 2017
That is the correct way of defining multiple output arguments, but really more then seven or eight input/output positional arguments is not a very good practice...
"I do not get the output arguments is spite of having them declared"
And that is exactly why.
You would be much better off putting all of those values into one structure, and simply passing that. Using one structure would be simple, neat, efficient, robust, clear, and easy to understand.
Could you please demonstrate how I will do this by doing an example ?
No single function should ever require that many outputs. You don't need to go quite to the extreme of a function to 'Do one thing and do it well', but functions should be as focussed as possible and use many functions to achieve a more complex outcome.
Your function takes 27 outputs (yes, I don't know why I bothered to count them!) which is way more than any function should ever need. You should never be wanting to call one function and ask it to do 27 things!
@Stephen Cobeldick I tried to design what you proposed.
This is what I did:
output = struct('name', {'Ext'; 'Flx'; 'Stft_ext'; 'Stft_flx'; 'Rms_ext'; 'Rms_flx';...
'Smoothed_ext'; 'Smoothed_flx'});
But I get the following error: Expected one output from a curly brace or dot indexing expression, but there were 8 results.
What do I wrong and how should I correct ?
Stephen23
Stephen23 il 4 Lug 2017
Modificato: Stephen23 il 4 Lug 2017
@Ioannis Agalliadis: the code you show above works without error and you did not give the complete error message.
I don't know what you are takling about. I sent you what is happening. I tried to invoke by typing output.name(2) and this is what it comes out : Expected one output from a curly brace or dot indexing expression, but there were 8 results.
"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.

Accedi per commentare.

 Risposta accettata

Jan
Jan il 4 Lug 2017
Modificato: Jan il 4 Lug 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 Commenti

I edited my previous comment with this new one:
function Out = preprocessing_data(Ext_dim,Flx_dim)
Out = struct ('Ext',value,'Flx',value,'Stft_ext',value,'Stft_flx',value,'Rms_ext',value,'Rms_flx',value,...
'Smoothed_ext',value,'Smoothed_flx',value,'Haar_ext',value,'Haar_flx',value,...
'Sym4_ext',value,'Sym4_flx',value,'Sym8_ext',value,'Sym8_flx',value,'Db8_ext',value,...
'Bior13_ext',value,'Bior13_flx',value,'Coif3_ext',value,'Coif3_flx',value,...
'Coif4_ext',value,'Coif4_flx',value,'Amor_ext',value,'Amor_flx',value,...
'Bump_ext',value,'Bump_flx',value,'Morse_ext',value,'Morse_flx',value)
This is what I wrote based on what you proposed. On the value parameter what should I write since the output of these cell arrays is calculated later on the programm ? What I mean is that I cannot have the values beforehand.
Ioannis, no one told you to do that - call struct(). Simply do what Jan suggested. Here it is fleshed out in more detail. Adapt as needed:
function Out = preprocessing_data(Ext_dim, Flx_dim)
try
Out.Ext = rand(); % Whatever.....
Out.Flx = rand(); % Whatever.....
Out.Stft_ext = rand(); % Whatever.....
Out.Stft_flx = rand(); % Whatever.....
Out.Rms_ext = rand(); % Whatever.....
Out.Rms_flx = rand(); % Whatever.....
Out.Smoothed_ext = rand(); % Whatever.....
Out.Smoothed_flx = rand(); % Whatever.....
Out.Haar_ext = rand(); % Whatever.....
Out.Haar_flx = rand(); % Whatever.....
Out.Sym4_ext = rand(); % Whatever.....
Out.Sym4_flx = rand(); % Whatever.....
Out.Sym8_ext = rand(); % Whatever.....
Out.Sym8_flx = rand(); % Whatever.....
Out.Db8_ext = rand(); % Whatever.....
Out.Bior13_ext = rand(); % Whatever.....
Out.Bior13_flx = rand(); % Whatever.....
Out.Coif3_ext = rand(); % Whatever.....
Out.Coif3_flx = rand(); % Whatever.....
Out.Coif4_ext = rand(); % Whatever.....
Out.Coif4_flx = rand(); % Whatever.....
Out.Amor_ext = rand(); % Whatever.....
Out.Amor_flx = rand(); % Whatever.....
Out.Bump_ext = rand(); % Whatever.....
Out.Bump_flx = rand(); % Whatever.....
Out.Morse_ext = rand(); % Whatever.....
Out.Morse_flx = rand(); % Whatever.....
catch ME
errorMessage = sprintf('Error in program %s.\n\nError Message:\n%s', ...
mfilename, ME.message);
uiwait(warndlg(errorMessage));
end
Jan
Jan il 4 Lug 2017
Modificato: Jan il 4 Lug 2017
In addition the "_flx" and "_ext" appendices looks like an additional level could be useful to reduce the number of fields to support the readability of the code. Try to keep similar data in one array. Perhaps one matrix called "Flex" and a cellstring containing the keys {'Stft', 'Rms', 'Smoothed', ...} is a leaner and more efficient representation. But this depends on the design of your program.
The idea is to reduce the optical complexity of the code. The easier the code is to read, the less prone is it to typos.
Thank you! Now I get it, how it is supposed to work.
@Jan Simon To be honest I did not get the suggestion you made about the matrix and cellstring. Could you elaborate more on that?
Stephen23
Stephen23 il 5 Lug 2017
Modificato: Stephen23 il 5 Lug 2017
@Ioannis Agalliadis: MATLAB works best when data is kept together as much as possible. This means keeping data in one matrix is usually preferable to creating lots of separate variables. It is also better to use the simplest array class possible: numeric if possible, cell if ragged data/different classes, structures only if named access is required, etc.
Because of the names that you have given your variables it indicates that you probably should put your data into arrays, rather than creating and accessing hundreds of separate variables. Accessing lots of variables will likely require complicated and hard-to-maintain code. Keeping data in one (or a few) arrays allows passing and parsing data by operating on the whole array at once.
Jan Simon suggested that you put your data into one array and store the labels separately. This would make your code simpler: in general simpler code is more efficient and less buggy.
@Stephen Codeblick: I understand the importance of putting the data in one or couple of matrices but I cannot understand how to realise it. Here I will paste some of the code and try to show me how you want me to implement it.
% _Declaring & initializing the variables_
Out.Ext = cell(Ext_dim,1);Out.Flx = cell(Flx_dim,1);
Out.Stft_ext = cell(Ext_dim,8);Out.Stft_flx = cell(Flx_dim,8);
% _Importing the data_
for i=1:Ext_dim
Out.Ext{i} = csvread(strcat('path_to_data',num2str(i),'.csv'));
end
for i=1:Flx_dim
Out.Flx{i} = csvread(strcat('path_to_data',num2str(i),'.csv'));
end
% _Applying STFT to the data_
for j=1:Ext_dim
for i=1:8
Out.Stft_ext{j,i} = num2cell(spectrogram(Out.Ext{j}(:,i+1)));
end
end
for j=1:Flx_dim
for i=1:8
Out.Stft_flx{j,i} = num2cell(spectrogram(Out.Flx{j}(:,i+1)));
end
end
Just some hints:
csvread(strcat('path_to_data',num2str(i),'.csv')) % Easier:
csvread(sprintf('path_to_data%d.csv', i))
I think the num2cell is misplaced in:
Out.Stft_flx{j,i} = num2cell(spectrogram(Out.Flx{j}(:,i+1)));
Now Out.Stft_flx{j,i} contains a scalar cell, which contains a vector. This looks more convenient:
Out.Stft_flx{j,i} = spectrogram(Out.Flx{j}(:,i+1));
I tried the first one:
Out.Ext{i} = csvread(sprintf('C:\Temp\MATLAB\repos\code\thumb_flex_extend\cutted\extensions\extension_%d.csv',i));
but it pops out: Error using csvread (line 35) File not found.
@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.

Accedi per commentare.

Più risposte (0)

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by