# How to call one of two conditions in matlab function?

5 views (last 30 days)
Rimvydas Voveris on 12 Oct 2021
Commented: Dave B on 12 Oct 2021
Hello, I want to ask how to call a condition x without condition b,because if i write a number array in my function it shows me condition b (it changes number array to vector column),but I want to get two different conditions,one of them would be x (it change number array o vector line) and b,but in my case if I write [x]=eilstulp([2 5 6 7]) I'm still getting b condition. So how to get codition b and second time get condition x without condition b?
My code:
function [b,x]=eilstulp(A)
%Eilstulp function which change number array to vector column or vector
%line
%[b,x]=eilstulp(A)
%b,x - output,
%b - Vector column rezult,
%x - Vector line rezult,
b=A';
b(:);
x=num2str(reshape(A',1,[]));

Dave B on 12 Oct 2021
Edited: Dave B on 12 Oct 2021
When you call a function with one output MATLAB will return the first output, the names specified for outputs when calling it are irrlevant. You can use a ~ to ignore an argument.
If you really wanted this style of function return arguments (i.e. return both always, specified by name), you could get close to it by returning a struct with fieldnames corresponding the the two outputs (see example below)
A = 1:10;
[col,rowstr]=eilstulp(A)
col = 10×1
1 2 3 4 5 6 7 8 9 10
rowstr = '1 2 3 4 5 6 7 8 9 10'
[~,rowstr]=eilstulp(A)
rowstr = '1 2 3 4 5 6 7 8 9 10'
res = eilstulp2(A);
res.b
ans = 10×1
1 2 3 4 5 6 7 8 9 10
res.x
ans = '1 2 3 4 5 6 7 8 9 10'
function [b,x]=eilstulp(A)
%Eilstulp function which change number array to vector column or vector
%line
%[b,x]=eilstulp(A)
%b,x - output,
%b - Vector column rezult,
%x - Vector line rezult,
b=A';
b(:);
x=num2str(reshape(A',1,[]));
end
function res=eilstulp2(A)
res.b=A(:);
res.x=num2str(reshape(A',1,[]));
end
Dave B on 12 Oct 2021
No need to apologise, we're here to help!