How to call one of two conditions in matlab function?

3 vues (au cours des 30 derniers jours)
Rimvydas Voveris
Rimvydas Voveris le 12 Oct 2021
Commenté : Dave B le 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,[]));

Réponse acceptée

Dave B
Dave B le 12 Oct 2021
Modifié(e) : Dave B le 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
  4 commentaires
Rimvydas Voveris
Rimvydas Voveris le 12 Oct 2021
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 le 12 Oct 2021
No need to apologise, we're here to help!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by