How to call one of two conditions in matlab function?

3 次查看(过去 30 天)
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
Dave B 2021-10-12
编辑:Dave B 2021-10-12
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
  4 个评论
Rimvydas Voveris
Rimvydas Voveris 2021-10-12
Thank you guys, you saved my life I'm not a programer and this is hard for me to always get the point,sry for my misunderstanding at the begining.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by