How to store vector name and value in the result

I have the following vectors and want to make it as table or array
Parameter1=1
Parameter2=2
Parameter3=3
the result will be
disp('| Parameter | Value ')
A=[Parameter1 1;Parameter2 2;Parameter3 3]

답변 (1개)

Aquatris
Aquatris 2018년 7월 22일

0 개 추천

Here is an example code;
xName = ['x1';'x2';'x3';'x4'];
xVal = [1;2;3;4];
A = table(xName,xVal);
Result look like this;
A =
xName xVal
_____ ____
x1 1
x2 2
x3 3
x4 4

댓글 수: 4

joms
joms 2018년 7월 22일
thanks for the answer. however theres an concatenation error when xName characters exceeds 2. is theres another way?
Aquatris
Aquatris 2018년 7월 22일
편집: Aquatris 2018년 7월 22일
My bad. Define the names using brackets instead (making xName a cell array);
xName = {'x1';'x2';'x3';'x45'};
However this will display 'x1' in the command window instead of x1. If you want to display like previous one does (xName is char array) you can do it like;
xName(1,1:2) = 'x1';
xName(2,1:3) = 'x12';
xName(3,1:5) = 'x1234';
xName(4,1:7) = 'x123456';
Here the column index determines the number of characters in the mae (x1 has 2 character so 1:2, x12 has 3 so 1:3 and so on).
Hope this helps.
joms
joms 2018년 7월 22일
it works but still gives me "'x1'" output i want "x1" only
Try the second method.
xName(1,1:2) = 'x1';
xName(2,1:3) = 'x12';
xName(3,1:5) = 'x1234';
xName(4,1:7) = 'x123456';

댓글을 달려면 로그인하십시오.

카테고리

태그

질문:

2018년 7월 22일

댓글:

2018년 7월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by