I want to achieve:
1. psm_q is a script.
2. When I am in main.m, type
psm_q;
psm_q will be shown in the workspace as a 6*1 matrix with values defined in psm_q.m
Let's say the 6*1 matrix of psm_q is [pi/3; pi/3; 0.1; pi/3; pi/3; pi/3], what should I code in psm_q?
I tried typing in psm_q:
[pi/3; pi/3; 0.1; pi/3; pi/3; pi/3]
,
[q1=pi/3;
q2=pi/3;
q3=0.1;
q4=pi/3;
q5=pi/3;
q6=pi/3]
and
function [q1,q2,q3,q4,q5,q6]=psm_q()
q1=pi/3;
q2=pi/3;
q3=0.1;
q4=pi/3;
q5=pi/3;
q6=pi/3;
end
but they all don't work as expected.
Please help.

1 Kommentar

Stephen23
Stephen23 am 10 Dez. 2018
Numbered variables are a sign that you are doing something wrong. Sitck to using arrays if you want to make your code simpler and more efficient.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

madhan ravi
madhan ravi am 10 Dez. 2018
Bearbeitet: madhan ravi am 10 Dez. 2018

0 Stimmen

Just call it in the main.m script
function psm_q=psm_q_func()
q1=pi/3;
q2=pi/3;
q3=0.1;
q4=pi/3;
q5=pi/3;
q6=pi/3;
psm_q=[q1 q2 q3 q4 q5 q6].';
end

4 Kommentare

Laurel Castillo
Laurel Castillo am 10 Dez. 2018
Thanks for your help!
But when the main.m has another function,
the error shows: Function with duplicate name "main" cannot be defined.
madhan ravi
madhan ravi am 10 Dez. 2018
there contains another file named main.m either rename it or delete it
Stephen23
Stephen23 am 10 Dez. 2018
Bearbeitet: Stephen23 am 10 Dez. 2018
Simpler without transpose:
psm_q = [q1; q2; q3; q4; q5; q6];
or even better:
function out = psm_q_func()
out = [pi/3; pi/3; 0.1; pi/3; pi/3; pi/3];
end
madhan ravi
madhan ravi am 10 Dez. 2018
Thank you @Stephen :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Stephen23
Stephen23 am 10 Dez. 2018
Bearbeitet: Stephen23 am 10 Dez. 2018

0 Stimmen

Actually all you need in your script is this:
psm_q = [pi/3; pi/3; 0.1; pi/3; pi/3; pi/3];

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by