How to call one of two conditions in matlab function?

조회 수: 3 (최근 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 CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by