How to call one of two conditions in matlab function?

2 ビュー (過去 30 日間)
Rimvydas Voveris
Rimvydas Voveris 2021 年 10 月 12 日
コメント済み: Dave B 2021 年 10 月 12 日
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.
Dave B
Dave B 2021 年 10 月 12 日
No need to apologise, we're here to help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by